开发者

Is it possible to remove all events attached to an element and its children

I need to be able to remove events att开发者_StackOverflow中文版ached to an element and all its children even when I don't know what these events are. All the events are attached using jquery.


For jQuery 1.8 and later, use

$(element).find("*").addBack().off();

addBack() adds the original list of elements from $(element) back to the internal collection of elements held by the jQuery object (those returned by find("*")). off() removes all attached event handlers, including those using delegation.

If you just want children and not all descendants, use

$(element).children().addBack().off();

See the documentation:

  • addBack()
  • off()

For jQuery 1.7 and lower, use andSelf() instead of addBack(). For jQuery 1.6 and lower, use unbind() and die() instead of off(). For example:

$(element).children().andSelf().unbind().die();


andSelf() function is deprecated.

Use add() function:

$(element).add("*").off();


Use the .unbind() method on the element.


This is not an answer, but it is worth mentioning.

I had used a third party plugin without a destroy method. I found this solution but it didn't work at the beginning until I started digging deeper into the documentation.

Note that if the plugin are creating a $(document).on(event, handler) listener on your object, you have to remove the action by writing $(document).off(event, 'element, element *'). Simply writing $(element).find("*").addBack().off(); won't work if the main listener is the document.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜