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 });
});
4

1 回答 1

1

尝试将本机 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();
});
于 2013-07-18T20:50:29.163 回答