从DOM树中删除元素的回调? [英] Callback on the removal of an element from the DOM tree?

查看:332
本文介绍了从DOM树中删除元素的回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个动态添加到页面的DOM元素,然后使用jQuery的 $(#id)。remove()删除,是否有可能设置一个回调,所以当元素被删除时,我可以执行一个自定义动作。



我正在考虑用jQuery修补jQuery的 remove( )方法,所以我可以检查元素的ID,如果它匹配我想要的,执行回调,但我不知道如何



有没有更好的方法来做到这一点?我该如何使用猴子补丁来做到这一点?



(我正在写一个grepmonkey脚本,我不能改变底层的实现)

您可以侦听 DOMNodeRemoved 事件,然后按您所需的元素进行过滤。

  $(body).bind(DOMNodeRemoved,function(e){

if(e.target.id ==id){
//做某事
}
});

像往常一样,有一个IE只需要注意,所以看看这个帖子: http:// www。 bennadel.com/blog/1623-Ask-Ben-Detecting-When-DOM-Elements-Have-Been-Removed-With-jQuery.htm


If I have a DOM element which is dynamically added to the page, and then removed using jQuery's $("#id").remove(), is it possible to setup a callback, so when the element is removed, I can execute a custom action.

I was thinking of monkey-patching jQuery's remove() method, so I can check the element's ID and if it matches the one I want, execute the callback, but I'm not sure how.

Are there any better ways to do it? And how can I do it using monkey-patching?

(I'm writing a greasemonkey script, and I can't change the underlying implementation)

解决方案

You can listen to DOMNodeRemoved event and then filter by your required element.

$( "body" ).bind("DOMNodeRemoved", function(e){

  if(e.target.id == "id") {
     // do something
  }
});

As usual, there is a IE only caveat, so check out this post: http://www.bennadel.com/blog/1623-Ask-Ben-Detecting-When-DOM-Elements-Have-Been-Removed-With-jQuery.htm

这篇关于从DOM树中删除元素的回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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