jQuery ordered actions
Does anybody know how to make a jQery function that has more than one action and every action will be fired only after its precedent is complete like:
$('#myelement').addClass('loading').load(loadUrl).removeClass('loading');
here the first action which is adding the class name is ok, the second is also ok, but the problem comes with the last action which is supposed to remove 开发者_开发知识库the class after the load is finished, but here it will be fired even before the loading is finished and will cancel the first action so that it will look like none of the first nor the third action are present.
Thanks.
Here you go:
$('#myelement').addClass('loading').load(loadUrl, function() {
$(this).removeClass('loading');
});
This assigns an anonymous function as a callback for the load
method, which will be invoked when the load operation is completed.
Try this instead:
$('#myelement').addClass('loading').load(loadUrl, function() { ($(this).removeClass('loading'); });
The .load() function takes a completion function.
精彩评论