问题描述
我有以下代码添加eventListener
I have the following code to add eventListener
area.addEventListener('click',function(event) {
app.addSpot(event.clientX,event.clientY);
app.addFlag = 1;
},true);
它按预期正常工作。在另一个函数中,我尝试使用以下代码
It is working correctly as expected..Later in another function i tried to remove the event listener using the following code
area.removeEventListener('click',function(event) {
app.addSpot(event.clientX,event.clientY);
app.addFlag = 1;
},true);
但是,即使是监听器也没有删除...为什么会发生?我的removeEventListener有什么问题()?
注意:这里的区域类似于document.getElementById('myId')
But the even listener is not removed..Why is it happening?Is there any problem with my removeEventListener()?Note:Here area is something like document.getElementById('myId')
推荐答案
这是因为这两个匿名功能是完全不同的功能。您的 removeEventListener
的参数不是以前附加的函数对象的引用。
This is because that two anonymous functions are completely different functions. Your removeEventListener
's argument is not a reference to the function object that was previously attached.
function foo(event) {
app.addSpot(event.clientX,event.clientY);
app.addFlag = 1;
}
area.addEventListener('click',foo,true);
area.removeEventListener('click',foo,true);
这篇关于Javascript removeEventListener不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!