【发布时间】:2010-12-24 18:17:17
【问题描述】:
如何添加一个也接受参数的事件监听器(addEventListener(),attachEvent())?
参数作为元素的自定义属性传递,如:
<img src="icon.gif" alt="Test button" command="test" />
<img src="icon2.gif" alt="Test button2" command="write" />
【问题讨论】:
如何添加一个也接受参数的事件监听器(addEventListener(),attachEvent())?
参数作为元素的自定义属性传递,如:
<img src="icon.gif" alt="Test button" command="test" />
<img src="icon2.gif" alt="Test button2" command="write" />
【问题讨论】:
您可以在处理程序中使用 getAttribute,例如
var param = this.getAttribute('command');
【讨论】:
你可以这样使用:
element.addEventListener ( 'click', (function ( myParam ) {
return function () {
// user myParam here
};
} ) ( yourParam ), false );
无论您以“yourParam”的形式传入什么,事件处理程序都可以通过“myParam”参数访问...
【讨论】: