我想加载html,然后获取图片的真实高度。
$("#cover").html(stuff); //this has the image inside it
$(function(){
console.log($("#cover img").height()); //sometimes it's 0
});
由于某些原因,有时高度为0。会不会是因为图片还没有完全加载,而JQuery 认为它是0?如果是这样,我该如何克服这个问题?图片加载的时候有回调什么的吗?
最佳答案
可以使用jQuery的.load()方法
$('#cover').html(stuff);
$('#cover img').load(function () {
console.log($(this).height());
};
关于javascript - 如何使用JQuery获取真实图像高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10577418/