例如 <img src=”bag001.jpg” id=”preview” />,没有也可以,但找起来比较麻烦。
$(‘#preview’).width();
$(‘#preview’).height();
上面这个宽和高是你看到的图片的大小,不一定是图片本身的真实尺寸(有可能在 HTML 中你设置了 width 和 height)。
要得到图片的真实尺寸:
1 2 3 4 5 6 7 |
var imgWidth, imgHeight; // 这里创建一个图像保存到内存,并没有添加到 HTML 中,只是个参考 $("<img/>").attr("src", "http://www.example.com/images/bag001.jpg").load(function() { imgWidth = this.width; imgHeight = this.height; }); // 现在 imgWidth 和 imgHeight 就是图像真实的尺寸 |