簡體   English   中英

jQuery .width()chrome和safari問題

[英]jquery .width() chrome and safari problem

我已經在css中將圖像的高度設置為某個值,並且寬度會自動修改,以便保留寬高比。 在CSS調整圖像大小后,我正在使用.width()jQuery函數獲取圖像的寬度。 在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;    
}

document.ready (您當前正在使用)中,可能會或可能不會加載圖像,如果它們不在緩存中,則可能不會加載。 而是使用window.onload事件時的圖像加載,就像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM