简体   繁体   English

如何将参数传递给作为addEventListener的参数传递或被分配给事件处理程序的函数?

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

将参数添加到传递给addEventListener()或直接分配给事件处理程序的函数的正确和专业方法是什么,如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); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果在初始化函数中分配了事件处理程序,如何传递参数? - How to pass argument if event handler is assigned in initialization function? 如何将参数传递给addEventListener中传递的侦听器函数? - How to pass an argument to the listener function passed in addEventListener? Javascript addEventListener :将参数传递给外部事件处理程序 - Javascript addEventListener : pass argument to external event handler 如何将参数传递给该匿名函数? - How are parameters being passed to this anonymous function? 如何在JavaScript中的函数(本身就是一个参数)中传递参数 - How argument is being passed in the function(which itself is an argument) in JavaScript javascript - 将事件和参数传递给使用 addEventListener 调用的 function - javascript - Pass Event and Parameters to function called with addEventListener 需要解释函数表达式如何作为函数声明中的参数传递 - Need an explanation on how a function expression is being passed as an argument in a function declaration 如何将图像对象传递给为其分配的onclick函数? - How do I pass the image object into the onclick function being assigned to it? 如何将这个命名的自调用函数传递给参数? - How is this named self-invoked function being passed an argument? JavaScript:如何将对象作为参数传递给addEventListener函数? - JavaScript : How to pass object as argument to addEventListener function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM