How to pass arguments to addEventListener listener function?

前端 未结 30 2914
谎友^
谎友^ 2020-11-21 23:56

The situation is somewhat like-

var someVar = some_other_function();
someObj.addEventListener(\"click\", function(){
    some_function(someVar);
}, false);
<         


        
30条回答
  •  感动是毒
    2020-11-22 00:50

    The following code worked fine for me (firefox):

    for (var i=0; i<3; i++) {
       element = new ...   // create your element
       element.counter = i;
       element.addEventListener('click', function(e){
            console.log(this.counter);
            ...            // another code with this element
       }, false);
    }
    

    Output:

    0
    1
    2
    

提交回复
热议问题