繁体   English   中英

加载后的图像宽度返回0

[英]Image width after load returns 0

有人可以帮我弄清楚我在做什么错吗? IE8会针对这些图像的宽度返回0。 在其他地方都可以正常工作。

$('#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以避免缓存问题:

$('#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();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM