【发布时间】:2011-03-01 12:47:05
【问题描述】:
现在我在 img 的 onload 函数时获得图像的宽度和高度。我的问题是,图像原始宽度 = 500px 但 document.getElementId(id).offsetWidth 只给出 300px 和高度。请帮助我如何获得图像的原始宽度和高度
【问题讨论】:
标签: javascript
现在我在 img 的 onload 函数时获得图像的宽度和高度。我的问题是,图像原始宽度 = 500px 但 document.getElementId(id).offsetWidth 只给出 300px 和高度。请帮助我如何获得图像的原始宽度和高度
【问题讨论】:
标签: javascript
至少 Gecko(Firefox)和基于 WebKit(Safari、Chrome)的浏览器应该为图像元素实现属性 naturalWidth 和 naturalHeight:
var image = document.getElementById("imageid");
if (image.naturalHeight === undefined) {
alert("Your browser doesn't support this example!");
} else {
var message = "The original size of the image:";
message += "\n height: " + image.naturalHeight + "px";
message += "\n width: " + image.naturalWidth + "px";
alert(message);
}
【讨论】:
对不起,我的浏览器不支持 naturalHeight 和 naturalWidth..
【讨论】: