【问题标题】:jquery .width() chrome and safari problemjquery .width() chrome 和 safari 问题
【发布时间】:2011-04-30 15:32:26
【问题描述】:

我已在 css 中将图像的高度设置为某个值,并且宽度会自动修改,以便保留纵横比。我正在使用 .width() jquery 函数在 css 调整图像大小后获取图像的宽度。在 Firefox 和 Internet Explorer 中这很好用,但在 chrome 和 safari 中,每个图像返回的宽度始终为 0。我正在使用的代码如下:

var total=0; 
var widthdiv=0;
$(function(){
    $('.thumb').each(function(){
       widthdiv=$(this).width();
       total+=widthdiv;
       alert(widthdiv); //added so i can see what value is returned.
    });
});

以及图片的css:

#poze img{
    height:80px;
    width:auto;
    border:0px;
    padding-right:4px;    
}

【问题讨论】:

标签: jquery google-chrome safari


【解决方案1】:

document.ready(您当前正在使用)中,图像可能会或可能不会被加载,如果它们不在缓存中,它们可能没有被加载。而是在图像加载时使用window.onload event,如下所示:

var total=0; 
$(window).load(function() {
  $('.thumb').each(function() {
    total += $(this).width();
  });
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
相关资源
最近更新 更多