admin管理员组

文章数量:1202959

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

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

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

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

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

$(foo).remove( function() {
   stepb();
});
Share Improve this question asked Dec 31, 2012 at 20:21 Jason WellsJason Wells 8896 gold badges16 silver badges33 bronze badges 5
  • Perhaps remove().done(function(){ stepb() }); – mplungjan Commented Dec 31, 2012 at 20:24
  • check this stackoverflow.com/questions/7594817/jquery-remove-callback – Mehmet Ataş Commented Dec 31, 2012 at 20:24
  • 2 Have to ask. Why not just $(foo).remove(); stepb();? It is an issue with the context of stepb()? – Jonathan Lonowski Commented Dec 31, 2012 at 20:34
  • Jonathan, I want to assure remove has completed before stepb is executed. – Jason Wells Commented Dec 31, 2012 at 22:34
  • 2 @JasonWells .remove() is a synchronous operation. The elements will already be detached from the document when it returns and before stepb() is called. – Jonathan Lonowski Commented Jan 1, 2013 at 7:48
Add a comment  | 

2 Answers 2

Reset to default 20

Try this

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

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

​Since the remove method in jQuery is synchronous, stepb() will be invoked after remove() has finished. So, no need to use $.when().then().

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

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

本文标签: javascriptjQuery callback within the remove() functionStack Overflow