我必须读取每个图像的自然高度,我应该进行计算。但是,我的阅读至自然高度有一些问题。
$('div.imgkirp img').each(function(){
console.log($(this).naturalHeight);
});
它获取:(图像编号)在控制台日志中未定义。我如何读取每个图像的自然高度?
尝试使用jQuery的prop
方法来使用图像的属性naturalHeight
:
$('div.imgkirp img').each(function(){
console.log($(this).prop('naturalHeight'));
});
var image = new Image();
image.src = $(this).attr("src");
alert('width: ' + image.naturalWidth + ' and height: ' + image.naturalHeight);
这种方法在Internet Explorer 8及以下版本中不起作用,因为它不支持"天然宽度"one_answers" NaturalHeight"属性。要实现同样的事情,请使用此代码:
var image = new Image();
image.src = $(this).attr("src");
image.onload = function() {
console.log('height: ' + this.height);
};