我尝试过这样的东西:
var Height = $(this).naturalHeight;
但它不起作用。有没有办法做到这一点?
格雷兹
发布于 2009-12-02 21:33:03
$this.css('height', 'auto');
var height = $this.height();
发布于 2010-07-15 22:29:16
在我看来,被接受的解决方案修改了对象的外观。有时候,jQuery有点太有帮助了,你必须告诉它让它给你让路。如果您想使用naturalWidth或naturalHeight,那么只需通过将jQuery对象转换为本机浏览器元素引用来使用已经存在的对象。
var Height = document.getElementById($(this).attr("id")).naturalHeight;
发布于 2009-12-02 21:42:53
获取图像尺寸的一种方法是使用javascript动态加载图像的副本并获取其尺寸:
// preload the image
var height, width = '';
var img = new Image();
var imgSrc = '/path/to/image.jpg';
$(img).load(function () {
alert('height: ' + img.height);
alert('width: ' + img.width);
// garbage collect img
delete img;
}).error(function () {
// image couldnt be loaded
alert('An error occurred and your image could not be loaded. Please try again.');
}).attr({ src: imgSrc });
https://stackoverflow.com/questions/1832907
复制相似问题