Internet Explorer 会自动将高度和宽度属性添加到新追加的图像中

Internet Explorer adds height- and width-attributes to a newly appended image automatically

本文关键字:新追加 追加 图像 添加 属性 Explorer 高度 Internet      更新时间:2023-09-26

>我在将新创建的映像加载到 DOM 后附加它:

var i = $('<img/>');
i[0].src = 'http://placehold.it/700x300';
i.attr('alt', '');
i.on('load', function() {
    $('body').append(i);            
});

我已经为CSS中的图像设置了固定高度:

img {
    height: 150px;
}

不幸的是,Internet Explorer将widthheight属性添加到图像中,因此它变得严重失真。我该如何防止这种情况?附加元素后是否必须手动删除属性?

js小提琴链接

试试这个:

img {
    height: 150px;
    width: auto;
}

您可以在 css 中添加 !important,也可以删除宽度和高度属性。

img {
    height: 150px !important;
}

i.height('').width('');