2013-07-07 13 views
0
<script type="text/javascript" src="jquery-1.4.full.js"></script> 
<body> 
</body> 
<script type="text/javascript"> 
var jdialog = $('<div id="avatar_button_clipdiv" style="width: 668px; height: 425px">\ 
<img width="668" height="900" src="http://localhost/1_tmp.jpg" style="">\ 
</div>').appendTo('body'); 
    console.log(jdialog.find('img')); 
    console.log(jdialog.find('img').width(),jdialog.find('img').height()); 
</script> 

此代碼工作正常返回0,高度()返回正確的值,而不是0,但是當我把它放回完整的HTML文件,它返回0而不是它的真實值,它是被修改的,並且不知道什麼可能使它的行爲有所不同。Jquery.height()爲img標籤,我把它獨自在另一個文件後

我編輯了一些CSS文件,然後運行此代碼,不記得我編輯了哪些css行。選擇器是正確的,我檢查了返回對象,無論是在單獨的代碼還是原始的HTML文件。任何人都可以在使用$()。height()時瞭解一下css更改可能會產生這種差異。

編輯

我在下面的CSS代碼已經點後,我評論他們,高度可以正確檢索,但仍然不知道爲什麼發生這種情況,這裏是代碼

img { 
    width: auto\9; 
    height: auto; 
    max-width: 100%; 
    vertical-align: middle; 
    border: 0; 
    -ms-interpolation-mode: bicubic; 
} 

身高:自動;是我已經commoneted,但如何是高度:汽車影響$()。高度();

回答

0

你自身共享的代碼似乎做的工作 我甚至在jsfiddle link運行它:

var jdialog = $('<div id="avatar_button_clipdiv" style="width: 68px; height: 125px">\ 
<img width="50" height="100" src="https://www.google.co.il/images/srpr/logo4w.png" style=""/>\ 
</div>').appendTo('body'); 
console.log(jdialog.find('img')); 
console.log(jdialog.find('img').width(),jdialog.find('img').height()); 

這是否happes在一個特定的瀏覽器?

+0

不,它在所有瀏覽器中都是一樣的,上面的代碼工作正常,但是在我將它放回到原始html文件後,高度爲0,寬度()雖然正確,但我不知道爲什麼 – user2556058

0
var $img = jdialog.find('img');  
$img.load(function(){ 
    console.log($img.height()); 
}); 
+0

沒有任何回報,即使不是0 – user2556058

0

當一切都失敗了閱讀的實際高度屬性,像這樣:

var height = jdialog.find('img').attr('height'); 

但是如果你需要真正的計算高度使用imagesloaded執行測量功能,只有當需要的圖像是滿載嘗試。 傳統的「加載」或「準備就緒」事件與緩存圖像分離。

相關問題