Jquery 在Internet Explorer中将PNG图像替换为透明背景

Jquery 在Internet Explorer中将PNG图像替换为透明背景,jquery,internet-explorer,png,background,transparent,Jquery,Internet Explorer,Png,Background,Transparent,image1.png和image2.png都具有透明背景 <script type="text/javascript"> $(document).ready(function(){ $("img#test").click(function() { $(this).attr("src", "image2.png"); }); }); </script> <img id="test" src="im

image1.png和image2.png都具有透明背景

<script type="text/javascript">
    $(document).ready(function(){
        $("img#test").click(function() {
            $(this).attr("src", "image2.png");
        });
    });
</script>
<img id="test" src="image1.png">

$(文档).ready(函数(){
$(“img测试”)。单击(函数(){
$(this.attr(“src”,“image2.png”);
});
});
上面的代码应该用image2.png替换image1.png。但在InternetExplorer6/7/8(兼容性视图关闭)中,它不会从背景中清除image1.png,而是将image2.png置于image1.png之上

在替换为image2.png之前,如何从背景中完全动态删除image1.png

编辑

我在base.css中使用了以下代码来修复在InternetExplorer中显示tranparent.png图像时的错误

/*Png透明*/

.mypng img{

方位角:表达式(

this.pngSet?this.pngSet=true:(this.nodeName==“IMG”&&this.src.toLowerCase().indexOf('.png')>-1?(this.runtimeStyle.backgroundImage=“无”

this.runtimeStyle.filter=“progid:DXImageTransform.Microsoft.AlphaImageLoader(src=”+this.src+”,sizingMethod='image')”

this.src=“/static/images/transparent.gif”):(this.origBg=this.origBg?this.origBg:this.currentStyle.backgroundImage.toString().replace('url(','')。replace(''),'')

this.runtimeStyle.filter=“progid:DXImageTransform.Microsoft.AlphaImageLoader(src=”+this.origBg+”,sizingMethod='crop')”

this.runtimeStyle.backgroundImage=“无”)),this.pngSet=true)

}


这就是问题的原因吗?

您可以尝试使用
$(this).after()
并在要替换的图像后添加新图像;最后执行
$(this.remove()

替换.png修复代码后,前一个代码可以工作。

必须有更多的内容。我不能复制这个。你也在谈论“背景”,你确定你没有任何CSS可以做到这一点吗?@BalusC:IE可以做任何事情:)@Andreas:我在IE6/7/8中用两个不同的透明PNG图像测试了这段代码片段,效果很好(除了aplha通道在IE6中不存在,但这是另一个问题)。@BalusC,我在页面的样式表中添加了一些代码,以修复显示透明.png图像的错误。可能是问题的原因?这是可行的,但在切换图像时会导致一些闪烁。