在铬问题中使用jQuery的图像高度 [英] image height using jquery in chrome problem

查看:112
本文介绍了在铬问题中使用jQuery的图像高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在chrome中返回 0 ,但它返回实际的高度在IE和firefox中。



什么是在chrome中获取图片高度的实际方法?

解决方案

正如Josh提到的,如果图像尚未完全加载,jQuery将不知道尺寸是什么。尝试类似这样的操作,以确保在图像完全加载后您只尝试访问它的尺寸:

  var img =新图片(); 

$(img).load(function(){
// - 您可以在添加到DOM
alert(height:+ img。高度);
// - 或者你可以先添加它...
$('body')。append(img);
// - 然后检查
alert($('body img')。height());
})。error(function(){
// - 如果URL无效或其他加载
alert(DANGER!... DANGER!...);
});

$(img).attr('src',http://sstatic.net/so/img/logo.png);


$('img').height() returns 0 in chrome, but it returns the actual height in IE and firefox.

Whats the actual method to get the height of the image in chrome?

解决方案

As Josh mentioned, if the image has not fully loaded yet, jQuery won't know what the dimensions are yet. Try something like this to ensure you're only attempting to access it's dimensions after the image has been completely loaded:

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的图像高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆