我想制作一个eventHandler
传递事件和一些参数的对象。问题是函数没有得到元素。下面是一个例子:
doClick = function(func){
var elem = .. // the element where it is all about
elem.onclick = function(e){
func(e, elem);
}
}
doClick(function(e, element){
// do stuff with element and the event
});
在elem
必须之外定义匿名函数。如何让传递的元素在匿名函数中使用?有没有办法做到这一点?
那么addEventListener
呢?我似乎根本无法通过事件传递,addEventListener
是吗?
更新
我似乎用“这个”解决了这个问题
doClick = function(func){
var that = this;
this.element.onclick = function(e){
func(e, that);
}
}
这包含this.element
我可以在函数中访问的地方。
addEventListener
但我想知道addEventListener
:
function doClick(elem, func){
element.addEventListener('click', func(event, elem), false);
}