jquery .width()chrome和Safari浏览器问题

问题描述:

我已经在css中设置了图像的高度为一定的值,并且宽度会自动修改,以便保持高宽比。我使用.width()jquery函数在css调整图像大小后获取图像的宽度。在Firefox和Internet Explorer中,这种方式很好,但在Chrome和Safari中,每个图像返回的宽度始终为0。我使用的代码如下:jquery .width()chrome和Safari浏览器问题

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;  
} 
+0

看一看[这太问题(http://stackoverflow.com/questions/318630/get-real-image-width -and-高度与JavaScript的功能于野生动物园,铬)。我认为你的问题是一样的。 – lonesomeday 2010-10-26 09:18:28

document.ready(你目前正在使用的)图像可能会或可能不会被加载,如果他们不在缓存中,他们可能不是加载。而是使用window.onload event的图像时,装,像这样:

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

完美运作。 – 2013-02-26 08:49:54

+0

width()也没有给我正确的值,如何解决它? http://stackoverflow.com/questions/18650991/phantom-height-when-specifying-the-width-of-a-li-using-jquery – mrN 2013-09-06 06:15:10