微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

图像高度使用jquery在chrome问题

$(‘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");

原文地址:https://www.jb51.cc/jquery/179315.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐