加载后的图像宽度返回0 [英] Image width after load returns 0
本文介绍了加载后的图像宽度返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮我弄清楚我在做错了什么吗?无论如何,IE8都会为这些图像的宽度返回0。在其他地方工作正常。
Can someone help me figure out what I'm doing wrong here? IE8 returns 0 for the width of these images regardless. Works fine everywhere else.
$('#hometown li img').load(function()
{
var imgWidth = parseInt($(this).width());
var percent = (99.99 * (imgWidth / 1600)) + '%';
console.log(imgWidth) // <- Always 0 in ie8
$(this).parent().css({ 'width': percent });
});
推荐答案
尝试单独将原生onload附加到每个图像,如果complete属性为true则触发onload以避免缓存问题:
Try attaching a native onload to each image seperately instead, and trigger the onload if the complete property is true to avoid caching issues :
$('#hometown li img').each(function() {
this.onload = function() {
var imgWidth = this.width();
var percent = 99.99 * (imgWidth / 1600);
console.log(imgWidth);
this.parentNode.style.width = percent + '%';
}
if(this.complete) this.onload();
});
这篇关于加载后的图像宽度返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文