$('img').height()
在 Chrome 中返回 0
,但在 IE 和 Firefox 中返回实际高度。
chrome 中获取图像高度的实际方法是什么?
最佳答案
正如 Josh 提到的,如果图像尚未完全加载,jQuery 将不知道尺寸是多少。尝试这样的操作,以确保您仅在图像完全加载后尝试访问其尺寸:
var img = new Image();
$(img).load( function() {
//-- you can determine the height before adding to the DOM
alert("height: " + img.height);
//-- or you can add it first...
$('body').append(img);
//-- and then check
alert($('body img').height());
}).error( function() {
//-- this will fire if the URL is invalid, or some other loading error occurs
alert("DANGER!... DANGER!...");
});
$(img).attr('src', "http://sstatic.net/so/img/logo.png");
关于jquery - chrome中使用jquery的图像高度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1743880/