[英]Set height and width of image inside div using javascript and jquery
[英]How to use height() on a div with an image inside that's using width: 100%
所以我有
div {
width: 20%;
}
img {
width: 100%;
}
<div>
<img />
</div>
因此,图像的缩放比例取决于div的宽度。 我想使用jQuery查找图像的高度。
var imgHeight = $(img).height();
console.log(imgHeight);
但是,它返回为0(div的高度)。 如何获得图像的渲染高度?
谢谢!
正如loganfsmyth所说,您需要等待图像完成加载。
您是否将代码放在document.ready中?
$(document).ready(function() {
var imgHeight = $(img).height();
console.log(imgHeight);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.