开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜