页面加载期间 IE 中没有图像的高度和宽度

No height and width of images in IE during page load

本文关键字:图像 高度 加载 IE      更新时间:2023-09-26
jQuery(window).load(function() {
    jQuery.each(jQuery(".extraimgs"), function() {
        //this.height = 0 and this.width=0 in IE only
        if (this.height > this.width) {
            if (this.height > 263) {
                this.height = 263;
            }
            if (this.width > 354) {
                this.width = 354;
            }
        }
        else {
            this.height = 263;
            this.width = 354;
        }
    });
});

网页代码

<asp:DataList runat="server" ID="dlImages" RepeatColumns="2" RepeatDirection="Horizontal" RepeatLayout="Table" OnItemDataBound="dlImages_ItemDataBound" Style="display: none;">
    <ItemTemplate>
        <div>
            <asp:Image runat="server" ID="imgPics" class="extraimgs" />
        </div>
    </ItemTemplate>                                 
</asp:DataList>

我在IE中的这段代码有问题。在这里,我在运行时(页面加载事件 - 使用 jQuery)期间设置图像的高度和宽度,并使用代码隐藏设置图像源。

此代码在除 IE8 以外的所有浏览器中都运行良好。在页面加载事件中,我获取图像的高度和宽度,但我在IE8中没有获得图像的高度和宽度。在加载事件期间,我尝试了很多方法来获取图像的高度和宽度,但它不起作用。

有谁知道解决方案?

好的,根据我上面的评论,这是一个应该可以工作的版本:

jQuery(function() {
    jQuery(".extraimgs").load(function() {
        var $this = $(this);
        if ($this.height > $this.width) {
            if ($this.height > 263) {
                $this.height = 263;
            }
            if ($this.width > 354) {
                $this.width = 354;
            }
        }
        else {
            $this.height = 263;
            $this.width = 354;
        }
    });
});

试试这个:

jQuery(document.body).load(function () {
      jQuery(".extraimgs").each(function (i) {
        if (this.height > this.width) {
            if (this.height > 263) {
                this.height = 263;
            }
            if (this.width > 354) {
                this.width = 354;
            }
        }
        else {
            this.height = 263;
            this.width = 354;
        }
      });
    });
那么

$(document).load()$(document).ready()而不是$(window).load()呢?