如何使用JQuery删除父元素的子元素的所有事件处理程序 [英] How to remove all event handlers for child elements of a parent element using JQuery

查看:673
本文介绍了如何使用JQuery删除父元素的子元素的所有事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个特定的父节点,例如一个动态创建的模态div.向其中添加一堆动态html,然后将这些元素绑定到click,mouseover等事件后,便可以取消与模式div的子元素相关联的所有事件的绑定.在我的特定示例中,将模式div隐藏后,将其完全从dom中删除,然后在需要时从头开始重新创建.

Given a specific parent node, for example a dynamically created modal div. After adding a bunch of dynamic html to it and then binding those elements to click, mouseover, etc events, is there a way to un-bind all of the events associated with child elements of the modal div. In my specific example, once the modal div is hidden it is then removed from the dom entirely and then recreated from scratch each time it is needed.

我正在寻找一种不必跟踪所有特定绑定的方法,而只需使用一个调用即可说:获取所有具有绑定的子元素并将其关闭".

I am looking for a way to not have to track all of the specific bindings, but rather just use one call to say: get any children elements that have bindings and 'off' them.

注意:我可以验证从dom中删除元素然后重新创建它不会杀死绑定,因为打开和关闭模式div会导致绑定事件被触发与创建div相同的次数.我正在使用$(document).on('click', '#abc',function(e) {});绑定元素.

Note: I can validate that removing the element from the dom and then recreating it does not kill the binding as opening and closing the modal div causes the bound events to be fired the same number of times the div was created. I am using $(document).on('click', '#abc',function(e) {}); to bind elements.

推荐答案

您可以使用 unbind()如果您使用bind()附加事件.

You can use unbind() if you used bind() to attach the events.

  $('#foo').children().unbind();
  $('#foo').children('.class').unbind(); //You can give selector for limiting clildren

如果您使用on()绑定事件,请使用 off().

Use off() if you used on() to bind events.

 $('#foo').children().off();
 $('#foo').children('class').off();   //You can give selector for limiting clildren

这篇关于如何使用JQuery删除父元素的子元素的所有事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆