使用jQuery获取每个图像的自然高度 [英] Get Each Image Natural Height With jQuery

查看:122
本文介绍了使用jQuery获取每个图像的自然高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ?

推荐答案

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

这种方法在IE 8及以下版本中不起作用,因为它不支持
'naturalWidth'和'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

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

这篇关于使用jQuery获取每个图像的自然高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆