jQuery Ajax 等待所有图像加载完毕
我正在尝试使用 jQuery 执行 Ajax 调用,效果很好。我使用成功事件来显示数据。然而,一旦加载外部 HTML 文件,似乎就会触发成功。如果有大图像,它们在显示后会继续加载。有没有办法在全部加载完毕后显示内容?这是代码:
$('#divajax').html('<br><div style="text-align: center;"><img src="res/ajax-loader.gif"></div>');
$.ajax({
cache: false,
url: 'ajax/content.php',
success: function(data) {
$('#divajax').html(data);
}
});
I'm trying to perform an Ajax call with jQuery which is working well. I use the success event to display the data. It seems however that success gets triggered as soon as the external HTML file gets loaded. If there are large images they keep loading after they're shown. Is there a way to display the content after everything is fully loaded? Here is the code:
$('#divajax').html('<br><div style="text-align: center;"><img src="res/ajax-loader.gif"></div>');
$.ajax({
cache: false,
url: 'ajax/content.php',
success: function(data) {
$('#divajax').html(data);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
@alex 编写的插件由于某种原因对我不起作用......我不明白为什么。但他的代码确实激发了我想出一个更适合我的轻量级解决方案。它使用 jquery 承诺。请注意,与 @alex 的插件不同,这不会尝试考虑元素上的背景图像,而仅考虑 img 元素。
然后你可以像这样使用它:
希望这对某人有帮助。
请注意,使用上述代码时,如果其中一张图像出现错误(例如,因为 URL 错误),则无论如何都会解决 Promise 并忽略该错误。这可能是您想要的,但是,根据您的情况,如果图像加载失败,您可能想中止正在执行的任何操作。在这种情况下,您可以按如下方式替换 onerror 行:
并捕获如下错误:
The plugin that @alex wrote didn't work for me for some reason... I couldn't figure out why. But his code did inspire me to come up with a more lightweight solution that is working for me. It uses jquery promises. Please note that unlike @alex 's plugin, this doesn't attempt to account for background images on elements, only img elements.
Then you can use it something like this:
Hopefully that's helpful for someone.
Note that using the above code, if one of the images errors (eg because the URL was wrong), the promise is resolved anyway and the error is ignored. This might be what you want, but, depending on your situation, you might instead want to abort whatever you are doing if an image fails to load. In which case you could replace the onerror line as follows:
And catch the error like this:
您可以使用我的 jQuery 插件 waitForImages...
这会将内容加载到您的元素中,隐藏它,然后重新显示一次后代
img
元素已加载。You could use my jQuery plugin, waitForImages...
This will load the stuff into your element, hide it, and then reshow it once descendent
img
elements have loaded.您可以将某些内容绑定到加载事件以了解它们何时完成:
或者您可以使用此插件。
You can bind something to the load events to know when they're done:
Or you could use this plugin.
这会在每个 img 加载上分别触发:
我使用:
this triggers on every img load separately:
i used:
您可以使用
imagesloaded
插件JavaScript 和 jQuery,尝试在加载的内容上使用内部 ajaxsuccess
回调函数,并在imagesloaded
回调函数不触发时隐藏所有内容,请参见下面的示例代码You can use
imagesloaded
plugin that is work with JavaScript and jQuery, try to use inside ajaxsuccess
callback function on loaded content, and hide all content whileimagesloaded
callback function not trigger, see below sample code考虑使用 jquery 的 .load() ,
其中有以下描述:
Look into using jquery's .load()
Which has a description of :
试试这个代码。效果很好。
Try this code. It works fine.
当丹尼尔的解决方案中的插件找不到图像时,我遇到了一个小错误。
以防万一其他人遇到同样的问题:
更改:
到:
I've got a small bug when no images are found by the plugin from Daniel's solution.
Just in case some else get the same issue:
Change:
To :
我认为最好的就是使用这个
i think the best is using this