jQuery .load() 不适用于我的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3588102/
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
jQuery .load() not working on my image
提问by Etienne Dupuis
I have some code I am trying to run once my image has finished loading. I use this following jQuery code:
我有一些代码在我的图像加载完成后尝试运行。我使用以下 jQuery 代码:
$("#myimageid").load(function() {
alert('Image Loaded');
});
However the popup never show up. I can't get the .load() function to work ! Anyone had issues with this?
然而,弹出窗口永远不会出现。我无法让 .load() 函数工作!任何人有这个问题?
回答by Nick Craver
If you're running this afterthe image already has a set source, you need to do an additional check for caches images (who fired the event, just before you added an event handler listening for it). You can do that like this:
如果你在图像已经有一个设置的源之后运行这个,你需要对缓存图像进行额外的检查(谁触发了事件,就在你添加一个监听它的事件处理程序之前)。你可以这样做:
$("#myimageid").on('load', function() {
alert('Image Loaded');
}).each(function() {
if(this.complete) $(this).load();
});
Update for later versions of query, use:
更新查询的更高版本,使用:
if(this.complete) $(this).trigger('load');
Using (this).load();
will produce a Cannot read property 'indexOf' of undefined
error
使用(this).load();
会产生Cannot read property 'indexOf' of undefined
错误
回答by David Hoerster
What happens when you clear your browser's cache and try the script again? From the jQuery load()
docs:
当您清除浏览器的缓存并再次尝试脚本时会发生什么?来自 jQueryload()
文档:
It is possible that the load event will not be triggered if the image is loaded from the browser cache. To account for this possibility, we can use a special load event that fires immediately if the image is ready. event.special.load is currently available as a plugin
In general, it is not necessary to wait for all images to be fully loaded. If code can be executed earlier, it is usually best to place it in a handler sent to the .ready() method.
如果图像是从浏览器缓存加载的,则可能不会触发 load 事件。为了解决这种可能性,我们可以使用一个特殊的加载事件,如果图像准备好,它会立即触发。event.special.load 目前可作为插件使用
一般来说,没有必要等待所有图像完全加载。如果代码可以更早地执行,通常最好将它放在发送到 .ready() 方法的处理程序中。
So it sounds like your cached image isn't triggering the load event, so you may want to try the plug-in that's mentioned. You can find the JS file to download here.
所以听起来你的缓存图像没有触发加载事件,所以你可能想尝试提到的插件。您可以在此处找到要下载的 JS 文件。
Hope this helps!
希望这可以帮助!