使用jQuery获取每个图像的自然高度

我必须阅读每个图像的自然高度,我应该进行计算。 但是我对自然高度的读取有一些问题。

$('div.imgkirp img').each(function(){ console.log($(this).naturalHeight); }); 

它得到:(图像编号)在控制台日志中未定义。 我如何阅读每个图像的自然高度?

 var image = new Image(); image.src = $(this).attr("src"); alert('width: ' + image.naturalWidth + ' and height: ' + image.naturalHeight); 

这种方法在IE 8及以下版本中不起作用,因为它不支持’naturalWidth’和’naturalHeight’属性。 要实现相同的使用此代码

 var image = new Image(); image.src = $(this).attr("src"); image.onload = function() { console.log('height: ' + this.height); }; 

尝试使用jQueryprop方法使用属性naturalHeight of image

 $('div.imgkirp img').each(function(){ console.log($(this).prop('naturalHeight')); }); 

基本计算以获得自然宽度和自然高度。

 var width = $("img", this).css('width', 'auto').width(); var height= $("img", this).css('height', 'auto').height();