我试图弄清楚如何执行一些js代码时,一个元素从页面删除:

jQuery('#some-element').remove(); // remove some element from the page
/* need to figure out how to independently detect the above happened */

有没有专门的活动,比如:

jQuery('#some-element').onremoval( function() {
    // do post-mortem stuff here
});

当前回答

我不能得到这个答案与解绑定工作(尽管更新见这里),但能够想出一个办法。答案是创建一个“destroy_proxy”特殊事件,触发一个“destroyed”事件。你把事件监听器放在'destroyed_proxy'和'destroyed'上,然后当你想解除绑定时,你只需要解除'destroyed'事件的绑定:

var count = 1;
(function ($) {
    $.event.special.destroyed_proxy = {
        remove: function (o) {
            $(this).trigger('destroyed');
        }
    }
})(jQuery)

$('.remove').on('click', function () {
    $(this).parent().remove();
});

$('li').on('destroyed_proxy destroyed', function () {
    console.log('Element removed');
    if (count > 2) {
        $('li').off('destroyed');
        console.log('unbinded');
    }
    count++;
});

这里有一把小提琴

其他回答

你可以绑定到DOMNodeRemoved事件(DOM Level 3 WC3规范的一部分)。

适用于IE9,最新版本的Firefox和Chrome。

例子:

$(document).bind("DOMNodeRemoved", function(e)
{
    alert("Removed: " + e.target.nodeName);
});

您还可以通过绑定到DOMNodeInserted来获得元素插入时的通知

我喜欢mtkopone使用jQuery特殊事件的答案,但请注意,它不能工作a)当元素被分离而不是删除或b)当一些旧的非jQuery库使用innerHTML破坏你的元素

亚当的答案的扩展,以防你需要防止违约,这里有一个工作:

$(document).on('DOMNodeRemoved', function(e){
        if($(e.target).hasClass('my-elm') && !e.target.hasAttribute('is-clone')){
            let clone = $(e.target).clone();
            $(clone).attr('is-clone', ''); //allows the clone to be removed without triggering the function again

            //you can do stuff to clone here (ex: add a fade animation)

            $(clone).insertAfter(e.target);
            setTimeout(() => {
                //optional remove clone after 1 second
                $(clone).remove();
            }, 1000);
        }
    });

我不能得到这个答案与解绑定工作(尽管更新见这里),但能够想出一个办法。答案是创建一个“destroy_proxy”特殊事件,触发一个“destroyed”事件。你把事件监听器放在'destroyed_proxy'和'destroyed'上,然后当你想解除绑定时,你只需要解除'destroyed'事件的绑定:

var count = 1;
(function ($) {
    $.event.special.destroyed_proxy = {
        remove: function (o) {
            $(this).trigger('destroyed');
        }
    }
})(jQuery)

$('.remove').on('click', function () {
    $(this).parent().remove();
});

$('li').on('destroyed_proxy destroyed', function () {
    console.log('Element removed');
    if (count > 2) {
        $('li').off('destroyed');
        console.log('unbinded');
    }
    count++;
});

这里有一把小提琴

对于那些使用jQuery UI的人:

jQuery UI已经覆盖了一些jQuery方法来实现remove事件,该事件不仅在你显式删除给定元素时被处理,而且在元素被任何自清理jQuery方法(例如replace, html等)从DOM中删除时也会被处理。这基本上允许您在jQuery“清理”与DOM元素相关的事件和数据时,将钩子放入相同的事件中。

John Resig表示,他愿意在未来版本的jQuery核心中实现这个事件,但我不确定它目前的位置。