我必须读取每个图像的自然高度,并且我应该进行计算。然而,我对自然高度的阅读有一些问题。
$('div.imgkirp img').each(function(){
console.log($(this).naturalHeight);
});
它在控制台日志中得到:(图像编号)未定义。如何读取每个图像的自然高度?
发布于 2014-01-26 21:29:12
var image = new Image();
image.src = $(this).attr("src");
alert('width: ' + image.naturalWidth + ' and height: ' + image.naturalHeight);
这种方法在IE8及更低版本中不起作用,因为它不支持'naturalWidth‘和'naturalHeight’属性。要实现同样的效果,请使用以下代码
var image = new Image();
image.src = $(this).attr("src");
image.onload = function() {
console.log('height: ' + this.height);
};
发布于 2015-05-05 00:51:00
尝试使用图像的属性naturalHeight
,使用jQuery
的prop
方法
$('div.imgkirp img').each(function(){
console.log($(this).prop('naturalHeight'));
});
https://stackoverflow.com/questions/21363955
复制相似问题