Possible Duplicate:
Javascript - Function to return width/height of remote image from url
是否可以通过js知道图像位置来获取宽度和高度?实际图片未加载到 DOM 上。我一直在用 jQuery 尝试这个,但还没有成功。
var img = $('<img>').attr('src', 'images/logo.png');
console.log($(img).width());
返回 0。
最佳答案
不,图像不需要位于 DOM 中。
var img = new Image();
img.onload = function(){
console.log(this.width);
};
img.src="images/logo.png";
但是你必须在知道它的尺寸之前加载它
(注意onload函数后面的src)
这是一个jsfiddle:http://jsfiddle.net/fGUyz/2/
#NotMrDownVoter
关于Javascript - 告诉网页上尚未实现的图像的宽度/高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14674929/