我希望获取标签内定义的高度和宽度,而不是页面上显示的原始或实际高度。
例如
<img id="testImg" src="http://test.com/img.png" height="300" width="250">
Run Code Online (Sandbox Code Playgroud)
我期望返回 300 的高度和 250 的宽度。
在标签内未定义高度和宽度的示例中:
<img id="testImg" src="http://test.com/img.png">
Run Code Online (Sandbox Code Playgroud)
我希望高度和宽度都返回 false 或未定义或 0。
使用 JavaScript:
var img = document.getElementById("testImg");
console.log(img.naturalHeight);
console.log(img.height);
Run Code Online (Sandbox Code Playgroud)
img.naturalHeight 和 img.height 不返回标记内定义的内容。它们返回原始或实际图像高度。
javascript ×1