How to change onClick handler dynamically?

前端 未结 11 1438
清歌不尽
清歌不尽 2020-12-02 15:37

I\'m sure there are a million posts about this out there, but surprisingly I\'m having trouble finding something.

I have a simple script where I want to set the onC

相关标签:
11条回答
  • 2020-12-02 15:47

    If you want to pass variables from the current function, another way to do this is, for example:

    document.getElementById("space1").onclick = new Function("lrgWithInfo('"+myVar+"')");
    

    If you don't need to pass information from this function, it's just:

    document.getElementById("space1").onclick = new Function("lrgWithInfo('13')");
    
    0 讨论(0)
  • 2020-12-02 15:50

    Nobody addressed the actual problem which was happening, to explain why the alert was issued.

    This code: document.getElementById("foo").click = new function() { alert('foo'); }; assigns the click property of the #foo element to an empty object. The anonymous function in here is meant to initialize the object. I like to think of this type of function as a constructor. You put the alert in there, so it gets called because the function gets called immediately.

    See this question.

    0 讨论(0)
  • 2020-12-02 15:56

    Try:

    document.getElementById("foo").onclick = function (){alert('foo');};
    
    0 讨论(0)
  • 2020-12-02 15:56

    I tried more or less all of the other solutions the other day, but none of them worked for me until I tried this one:

    var submitButton = document.getElementById('submitButton');
    submitButton.setAttribute('onclick',  'alert("hello");');
    

    As far as I can tell, it works perfectly.

    0 讨论(0)
  • 2020-12-02 15:57

    Here is the YUI counterpart to the jQuery posts above.

    <script>
      YAHOO.util.Event.onDOMReady(function() {   
        document.getElementById("foo").onclick =  function (){alert('foo');};
      });
    </script>
    
    0 讨论(0)
  • OMG... It's not only a problem of "jQuery Library" and "getElementById".

    Sure, jQuery helps us to put cross-browser problems aside, but using the traditional way without libraries can still work well, if you really understand JavaScript ENOUGH!!!

    Both @Már Örlygsson and @Darryl Hein gave you good ALTARNATIVES(I'd say, they're just altarnatives, not anwsers), where the former used the traditional way, and the latter jQuery way. But do you really know the answer to your problem? What is wrong with your code?

    First, .click is a jQuery way. If you want to use traditional way, use .onclick instead. Or I recommend you concentrating on learning to use jQuery only, in case of confusing. jQuery is a good tool to use without knowing DOM enough.

    The second problem, also the critical one, new function(){} is a very bad syntax, or say it is a wrong syntax.

    No matter whether you want to go with jQuery or without it, you need to clarify it.

    There are 3 basic ways declaring function:

    function name () {code}
    
    ... = function() {code} // known as anonymous function or function literal
    
    ... = new Function("code") // Function Object
    

    Note that javascript is case-sensitive, so new function() is not a standard syntax of javascript. Browsers may misunderstand the meaning.

    Thus your code can be modified using the second way as

     = function(){alert();}
    

    Or using the third way as

     = new Function("alert();");
    

    Elaborating on it, the second way works almost the same as the third way, and the second way is very common, while the third is rare. Both of your best answers use the second way.

    However, the third way can do something that the second can't do, because of "runtime" and "compile time". I just hope you know new Function() can be useful sometimes. One day you meet problems using function(){}, don't forget new Function().

    To understand more, you are recommended read << JavaScript: The Definitive Guide, 6th Edition >>, O'Reilly.

    0 讨论(0)
提交回复
热议问题