How to pass arguments to addEventListener listener function?

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

    Why not just get the arguments from the target attribute of the event?

    Example:

    const someInput = document.querySelector('button');
    someInput.addEventListener('click', myFunc, false);
    someInput.myParam = 'This is my parameter';
    function myFunc(evt)
    {
      window.alert(evt.currentTarget.myParam);
    }

    JavaScript is a prototype-oriented language, remember!

提交回复
热议问题