获取隐藏的div内图像的高度 [英] Get height of image inside a hidden div

查看:61
本文介绍了获取隐藏的div内图像的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取隐藏的div容器内图像的高度/宽度,但是.height().width()都返回0(如预期).

i want to to get the height/width of an image inside a hidden div-containter, but .height() and .width() both returns 0 (like expected).


$('body').append('<div id="init"><img id="myimg" src="someimage.png" /></div>');
$('#init').hide();
$('#myimg').height(); // == 0
$('#myimg').width(); // == 0

如何获得正确的图像高度/宽度?我需要它来做出一些决定:-)

How can i get the correct height/width of the image? I need it to make some decisions :-)

最诚挚的问候, Biggie

Best regards, Biggie

推荐答案

是因为图像尚未加载.....

Its because the image has not loaded yet.....

$('body').append('<div id="init"><img id="myimg" src="something.png" /></div>');
$('#init').hide();

$('#myimg').bind("load",function(){
    alert(this.height) //works
})​

这里是一个测试: http://jsfiddle.net/WMpSy/

这篇关于获取隐藏的div内图像的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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