2017-04-07 4 views
2

高さがここでゼロになる理由を知っている人はいますか?div要素の高さがゼロを返します

$(document).ready(function() { 
 
       alert($("#hello").height()); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<img id="hello" class="browseIMG" src="https://udemy-images.udemy.com/course/750x422/64132_926c_10.jpg" />

+0

を... – Roy

+0

私が置かれていますコードをスニペットに入れますが、皮肉なことにコードは現在動作します。 – Tom

答えて

5

DOMが正常にロードされた後も、あなたがネットワークからでも画像の読み込み前の画像の高さを取得しているためです。

おそらく、イメージの読み込みイベントでそれを行う必要があります。

$('#hello').load(function() { 
    alert($("#hello").height()); 
}); 
+0

素晴らしい作品です!ありがとうございました !!!!! –

+0

@MathiasVerhoevenあなたは大歓迎です:) –

1

document.readyは、実際に読み込む前に要素の高さを戻しているため、機能しません。

alert($("#hello").height());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<img id="hello" class="browseIMG" src="https://udemy-images.udemy.com/course/750x422/64132_926c_10.jpg" />

0

あなたの要素がまだロードされていないので、この試してください:それは私の422の警告を与える

$("#hello").load(function() { 
 
       alert($("#hello").height()); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<img id="hello" class="browseIMG" src="https://udemy-images.udemy.com/course/750x422/64132_926c_10.jpg" />

関連する問題