在jquery中返回0的宽度

phi*_*hil 7 javascript css jquery

我有一些jquery脚本在窗口加载后运行

$(window).load( function() {
   $('.picture a img').each(function(index) {
       $(this).height(280);
       if ($(this).width() > $(this).height()) {
          alert("yes");
       } else {
          alert("no");
       }

   });
});
Run Code Online (Sandbox Code Playgroud)

当我知道一些图像应该是"是"时,我总是得到一个"不".当我在chrome中跟踪脚本时,我注意到$(this).width()总是返回0.我用谷歌搜索并且一些建议是图像没有加载但是这里window.load我已经使用了我认为应该在一切都已加载后运行的方法.有任何想法吗??

谢谢你提前帮助

Clo*_*ler 1

Chrome 对图像宽度的处理很奇怪。我在构建 jQuery 照片库时注意到了这一点。如果你的图像宽度没有在标签中专门设置,chrome将返回0.FF,IE将计算出宽度,但chrome不会。尝试实际设置宽度,看看是否能得到所需的结果。

<img width="200" src="..." />
Run Code Online (Sandbox Code Playgroud)