How to pass arguments to addEventListener listener function?

前端 未结 30 2317
谎友^
谎友^ 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:56

    Other alternative, perhaps not as elegant as the use of bind, but it is valid for events in a loop

    for (var key in catalog){
        document.getElementById(key).my_id = key
        document.getElementById(key).addEventListener('click', function(e) {
            editorContent.loadCatalogEntry(e.srcElement.my_id)
        }, false);
    }
    

    It has been tested for google chrome extensions and maybe e.srcElement must be replaced by e.source in other browsers

    I found this solution using the comment posted by Imatoria but I cannot mark it as useful because I do not have enough reputation :D

提交回复
热议问题