javascript 如何取消绑定特定 div 内的所有事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8589832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to unbind all events inside a particular div
提问by Rahul garg
I have a parent div , say with id="to-remove"
我有一个父 div ,用 id="to-remove" 说
This div has multiple children divs and each children div also can have in-turn multiple divs and each of them can have href and/or onclick events.
这个div有多个子div,每个子div也可以有多个div,每个div都可以有href和/或onclick事件。
Is there way to remove all these events from inside this parent div using jquery..?
有没有办法使用 jquery.. 从这个父 div 中删除所有这些事件?
回答by Emre Erkan
Did you try .unbind()
:
你有没有试过.unbind()
:
$('#to-remove *').unbind('click'); // just for click events
$('#to-remove *').unbind(); // for all events
And as of jQuery 1.7, the .on()
and .off()
methods are preferred to attach and remove event handlers on elements. (from .unbind()
documentation) So if you're using jQuery > 1.7.x then this would be better:
从 jQuery 1.7 开始,首选.on()
和.off()
方法来附加和删除元素上的事件处理程序。(来自.unbind()
文档)所以如果你使用 jQuery > 1.7.x 那么这会更好:
$('#to-remove *').off();
回答by Sudhir Bastakoti
Try:
尝试:
$('#to-remove *').unbind(); //or you could assign some class and do $('.some-class').unbind();