为什么不删除EventListener工作?

时间:2015-06-25 14:11:33

标签: events javascript-events listener

var mouseListener = button2.addEventListener("mouseover", function (e) {
count++;
...
if(count > 5) button2.removeEventListener("mouseover", mouseListener);
});

我不知道为什么代码不起作用?如何在javascript中使用removeEventListener ()

1 个答案:

答案 0 :(得分:0)

listener是您在function函数中传递的addEventListener

var mouseListener = function (e) {
    count++;
    ...
    if ( count > 5 ) {
        button2.removeEventListener("mouseover", mouseListener);
    }
}

button2.addEventListener("mouseover", mouseListener);

文档:
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener