Javascript:带有匿名函数的RemoveEventListener
如何使用 Anonym
函数和 removeEventListener()
删除事件监听器;
document.getElementById("object").onclick = function(e){
if(e && e.stopPropagation) {
e.stopPropagation();
} else {
e = window.event;
e.cancelBubble = true;
}
}
所以我有这段代码,所调用的函数必须是匿名的,我不知道为什么,但如果不是,则无法正常工作,也许是因为事件:|
但如果是匿名的我该如何删除它?
How to remove and event listener with an Anonym
function , with removeEventListener()
;
document.getElementById("object").onclick = function(e){
if(e && e.stopPropagation) {
e.stopPropagation();
} else {
e = window.event;
e.cancelBubble = true;
}
}
So I have this piece of code and the function what's called must be anonym I'dont know why but If it's not then doesn't works correctly, maybe beacuse of the event :|
But If it's anonym how can I remove it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,您还没有添加实际的事件侦听器,您只是使用要运行的函数填充了 onclick 变量。所以你应该能够使用这样的东西:
编辑
刚刚在 jsFiddle 中尝试过,我建议的方法有效。
Well you havent added an actual event listener, you have just populated the onclick variable with a function to be run. So you should be able to just use something like this:
EDIT
Just tried it in jsFiddle and what I suggested works.
给它一个空值即可,这是onclick未初始化时的起始值:
document.getElementById("object").onclick = null
Just give it a null value, which is the starting value when onclick is not initialized:
document.getElementById("object").onclick = null