我正在尝试使用它们的 load() 方法获取一些图像的长宽比。
$('img').load(function(){
alert(this.src);
alert($(this).width());
setAspectRatio(this);
});
(this.src) 告诉我我期望听到的内容。 ($(this).width()) 给我 0。
我在 load() 的文档中找到了这一点:
Caveats of the load event when used with images
A common challenge developers attempt to solve using the .load() shortcut is to execute a function when an image (or collection of images) have completely loaded. There are several known caveats with this that should be noted. These are:
- It doesn't work consistently nor reliably cross-browser
- It doesn't fire correctly in WebKit if the image src is set to the same src as before
- It doesn't correctly bubble up the DOM tree
- Can cease to fire for images that already live in the browser's cache
其中一些项目对我来说有点模糊,但这些图像是使用 get() 方法附加到元素中的,所以我最好的猜测是我的问题与此有关。
这看起来并不是什么不寻常的情况,但 Google 没有给我任何信息。这里有人遇到过这种情况吗?
请您参考如下方法:
您尝试过使用
$(window).load(function(){
//initialize after images are loaded
});
在页面完成关闭之前,此操作不会触发。然后,您可以获取您想要的任何图像并在其上使用您的脚本。
