jquery - chrome中使用jquery的图像高度问题

标签 jquery image google-chrome height

$('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/

相关文章:

jquery - 切换类而不读取之前的类

android - 在android中绘制图片

css - Chrome Dev Tools 声称样式会覆盖自己

javascript - Chrome 开发控制台中的自定义对象检查

javascript - Forefox 和 Chrome 之间的 HTMLCollection.namedItem() 行为差异?

javascript addEventListener 注册之前的更改

javascript - 如何查找单词在 div 中的位置(y 坐标)

javascript - 使绝对定位的div获取父div的尺寸

image - 如何阻止 Bing map 填充图像(返回的坐标多于查询的坐标)?

html - 图像比例调整大小