简体   繁体   English

为什么没有 removeEventListener 函数?

[英]Why doesn't removeEventListener function?

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

I don't why the code doesn't work?我不明白为什么代码不起作用? How do I use the removeEventListener () in Javascript?如何在 Javascript 中使用removeEventListener ()

The listener is the function you are passing in the addEventListener function.listenerfunction你传入addEventListener功能。

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

button2.addEventListener("mouseover", mouseListener);

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM