javascript remove() 函数中的 jQuery 回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14105766/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:43:31  来源:igfitidea点击:

jQuery callback within the remove() function

javascriptjquery

提问by Jason Wells

I need to call a function after a DIV has been removed from the page.

从页面中删除 DIV 后,我需要调用一个函数。

I have tried adding a callback like so, but no luck. Any suggestions?

我试过像这样添加回调,但没有运气。有什么建议?

$(foo).remove( function() {
   stepb();
});

回答by The Alpha

Try this

试试这个

$.when($('#foo').remove()).then(stepb());[Example1][1] and [Example2][2].

$.when($('#foo').remove()).then(stepb()); [例1] [1]和[示例2] [2]。

$('#foo').remove();
stepb();

?Since the removemethod in jQueryis synchronous, stepb()will be invoked after remove()has finished. So, no need to use $.when().then().

?由于removein的方法jQuery是同步的,完成stepb()后会被调用remove()。因此,无需使用$.when().then().

回答by Jason Whitted

By default there is no event fired when something is removed from the DOM.

默认情况下,从 DOM 中删除某些内容时不会触发任何事件。

Take a look at this question for some good workarounds: jQuery - Trigger event when an element is removed from the DOM

看看这个问题的一些好的解决方法: jQuery - 当一个元素从 DOM 中删除时触发事件