使用jquery获取图像宽度和高度 [英] Getting image width and height with jquery
本文介绍了使用jquery获取图像宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个非常简单的代码,令人烦恼地工作,对于我的生活,我不明白为什么它现在失败了:
I have a very simple code, which annoyingly was working and for the life of me I can not see why it is now failing:
function imageSize(img){
var theImage = new Image();
theImage.src = img.attr('src');
var imgwidth = theImage.width;
var imgheight = theImage.height;
alert(imgwidth+'-'+imgheight);
}
传递的img变量是img对象,来自:
The "img" variable being passed is the img object, obtained from:
jQuery('img')
.each(function(){
var imgsrc = jQuery(this);
imageSize(imgsrc);
});
推荐答案
图片可能尚未加载。所以,试试(未经测试):
The image may not have loaded yet. So, try (untested):
function imageSize(img){
var theImage = new Image();
$(theImage).load(function() {
var imgwidth = this.width;
var imgheight = this.height;
alert(imgwidth+'-'+imgheight);
});
theImage.src = img.attr('src');
}
这篇关于使用jquery获取图像宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文