开发者

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

开发者 https://www.devze.com 2023-01-27 16:21 出处:网络
What is the correct and professional way to add parameters to a function being passed to addEventListener() or or being directly assigned to an e开发者_Go百科vent handlers as in el.onclick = doSomethi

What is the correct and professional way to add parameters to a function being passed to addEventListener() or or being directly assigned to an e开发者_Go百科vent handlers as in el.onclick = doSomething(param);


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); 
0

精彩评论

暂无评论...
验证码 换一张
取 消