我有一个非常简单的代码,令人烦恼的是,它正在工作,但在我的一生中,我不明白为什么它现在失败了:
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 对象,从以下位置获得:
jQuery('img')
.each(function(){
var imgsrc = jQuery(this);
imageSize(imgsrc);
});
图片可能尚未加载。所以,尝试(未经测试):
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');
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)