我正在处理一个插件,它需要我在加载后获取图像的宽度和高度,而不管图像的尺寸是如何确定的。jQuery获取图像宽度/高度加载容器
<img src=".." width="500" height="500" /> <!-- works fine -->
<img src=".." style="width: 500px; height: 500px;" /> <!-- works fine -->
<img src=".." width="500" /> <!-- only gives me width -->
<img src=".." style="width: 500px;" /> <!-- only gives me width -->
<img src=".." style="width: 500px; height: auto;" /> <!-- only gives me width -->
<img src=".." /> <!-- doesn't work at all -->
我试图加载图像后获取其尺寸,但我只能够得到图像的实际大小,而不是在它的网页上显示的大小。
img.innerWidth();
我也曾尝试:
$('<img/>').hide().attr('src',img.src).load(function() {
img.Owidth = $(this).width();
img.Oheight = $(this).height();
}).appendTo($('body'));
和:
$("<img/>").attr("src", img.attr("src")).load(function() {
img.width = this.width;
img.height = this.height;
});
其中在得到做了很好的工作使用,以获得宽度/高度
代码IM我是图像的原始大小,但不是加载和显示时的大小。
插件或您的代码必须抓取width属性,而不是查找元素的宽度。 – Andy
你使用什么代码来获得宽度和高度? – Fenton
[看看这个](http://stackoverflow.com/questions/318630/get-real-image-width-and-height-with-javascript-in-safari-chrome) – Malkus