jQuery 等待 .load 完成,然后做一些事情

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

Wait for .load to finish and then do something

jquery

提问by Nathan

I'm loading an external script through .load('url')and need to wait for it to finish so I can update the source of an image. Sometimes load is fast enough that the image reload applies, but most of the time it takes too long and the image reload happens first, so the image doesn't actually change.

我正在加载一个外部脚本.load('url'),需要等待它完成才能更新图像的源。有时加载速度足够快以至于图像重新加载适用,但大多数时间它需要太长时间并且图像重新加载首先发生,因此图像实际上并没有改变。

回答by Love Dager

Add a callback function:

添加回调函数:

$('#yourElement').load('yourUrl.html', function() {
  /* When load is done */
});

More to read at http://api.jquery.com/load

更多内容请访问http://api.jquery.com/load

回答by Igor Dymov

loadmethod has callback function that is executed when the request completes. I think you should use it.

load方法具有在请求完成时执行的回调函数。我认为你应该使用它。

回答by Ali Habibzadeh

Your question is answered here on how to capture the success event of a load function: Checking Jquery ajax load success

关于如何捕获加载函数的成功事件,您的问题在这里得到解答: Checking Jquery ajax load success