How to pass parameters to a function being passed as an argument of addEventListener or being assigned to an event handler?

帅比萌擦擦* 提交于 2019-12-12 04:00:33

问题


What is the correct and professional way to add parameters to a function being passed to addEventListener() or or being directly assigned to an event handlers as in el.onclick = doSomething(param);


回答1:


You can use an anonymous function when you need to pass parameters to another, like this:

el.addEventListener("click", function() { doSomething(param); }, false); 

Whereas if it didn't need parameters, it would just be:

el.addEventListener("click", doSomething, false); 


来源:https://stackoverflow.com/questions/4305196/how-to-pass-parameters-to-a-function-being-passed-as-an-argument-of-addeventlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!