我想使用' addEventListener'从事件'中得到一些东西,但在使用之后我必须使用' removeEventListener'删除事件监听器。我该怎么办呢?
示例:
var elem = document.createElement('div');
// I need argument of *event*
elem.addEventListener('click', function(*event*){
console.log(*event*)
});
// after used, I have to remove it, but only named function can be removed
elem.removeEventListener('click', function(event){
});
// this way can be actually removed, but I need *event*
elem.removeEventListener('click', namedFunc);
如何解决此问题?
答案 0 :(得分:0)
我很好奇你这样做的方式,顺便说一句
function once(el, ev, cb){
el.addEventListener(ev, function f(event){
cb.call(event, event);
el.removeEventListener(ev, f);
}, false);
}