Kamis, 25 Desember 2014

jquery can’t get true width/height of an object in Chrome

this came up when i tried to align the image in the middle of a div. i need to calculate the height and the width of the image. but when i try to use  $('img').height() returns 0 in chrome, but it returns the actual height in IE and firefox.  So, Whats the actual method to get the height of the image in chrome?

the method is actually works in every browser, the only matter is whether you put an window.loador document.ready. at first i try the document.ready and the result as i explained above. after search on google, i found out that this method is not work in document.ready so  i put window.load and it works.

so, the code will be :

$(window).load(function(){
alert($('.img').height());
})

instead of

$(document).ready(function(){
alert($('.img').height());
})

Tidak ada komentar:

Posting Komentar