Javascript Jquery删除元素内的所有事件处理程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13945025/
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
Jquery Remove All Event Handlers Inside Element
提问by Nuvolari
I have a div element with several elements inside it like buttons and so forth which have event handlers attached to them. I know its possible to go:
我有一个 div 元素,里面有几个元素,比如按钮等等,它们有附加到它们的事件处理程序。我知道可以去:
$("#button1").off()
To remove the handler for a button but I would like to do something like this if possible:
要删除按钮的处理程序,但如果可能的话,我想做这样的事情:
$("#div1").removeChildHandlers();
Is there a native function in JQuery to do this or would I have to loop them all the elements and remove 1 by 1?
JQuery 中是否有本机函数来执行此操作,或者我是否必须循环所有元素并逐一删除?
回答by jfriend00
jQuery will do the looping for you for just the direct children:
jQuery 将只为直接子级为您执行循环:
$("#div1").children().off();
or if you want all descendants:
或者如果你想要所有的后代:
$("#div1").find("*").off();
回答by Akhil Sekharan
Does this help:
这是否有帮助:
$("#div1").find('*').off();
回答by Roko C. Buljan
Try with
试试
$("#div1 >* ").off();
Or:
或者:
$("#div1").find('button').off();
if you're talking about <button>elements
如果你在谈论<button>元素

