I have to read each images' natural height and I should make calculations. However I have some problem with read to natural height.
我必须阅读每个图像的自然高度,我应该进行计算。但是我对自然高度的读取有一些问题。
$('div.imgkirp img').each(function(){
console.log($(this).naturalHeight);
});
It gets : (images number) undefined in console log. How can i read each of images' natural height ?
它得到:(图像编号)在控制台日志中未定义。我如何阅读每个图像的自然高度?
2 个解决方案
#1
28
var image = new Image();
image.src = $(this).attr("src");
alert('width: ' + image.naturalWidth + ' and height: ' + image.naturalHeight);
This approach doesn't work in IE 8 and below versions, because it doesn't support 'naturalWidth' and 'naturalHeight' properties. To achieve the same use this code
这种方法在IE 8及以下版本中不起作用,因为它不支持'naturalWidth'和'naturalHeight'属性。要实现相同的使用此代码
var image = new Image();
image.src = $(this).attr("src");
image.onload = function() {
console.log('height: ' + this.height);
};
#2
22
Try to use property naturalHeight
of image, using the prop
method of the jQuery
尝试使用jQuery的prop方法使用属性naturalHeight of image
$('div.imgkirp img').each(function(){
console.log($(this).prop('naturalHeight'));
});
#1
28
var image = new Image();
image.src = $(this).attr("src");
alert('width: ' + image.naturalWidth + ' and height: ' + image.naturalHeight);
This approach doesn't work in IE 8 and below versions, because it doesn't support 'naturalWidth' and 'naturalHeight' properties. To achieve the same use this code
这种方法在IE 8及以下版本中不起作用,因为它不支持'naturalWidth'和'naturalHeight'属性。要实现相同的使用此代码
var image = new Image();
image.src = $(this).attr("src");
image.onload = function() {
console.log('height: ' + this.height);
};
#2
22
Try to use property naturalHeight
of image, using the prop
method of the jQuery
尝试使用jQuery的prop方法使用属性naturalHeight of image
$('div.imgkirp img').each(function(){
console.log($(this).prop('naturalHeight'));
});