2009-12-20 82 views
0

image1.png和image2.png都具有透明背景。在Internet Explorer中用透明背景替換PNG圖像

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

上面的代碼應該用image2.png代替image1.png。但在Internet Explorer 6/7/8(兼容性查看關閉)中,它不會從背景中清除image1.png,而只是將image2.png放在image1.png上。

如何在使用image2.png替換之前完全從背景中動態刪除image1.png?

編輯

我用下面的代碼在base.css解決在Internet Explorer顯示tranparent PNG圖片漏洞。

/*巴Ransparent */

.mypng IMG {

方位角:表達式(

this.pngSet this.pngSet =真:(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 =」none「)),this.pngSet = true);

}

這是問題的原因嗎?

+0

必須有更多的事情。我無法重現這一點。你也在談論「背景」,你確定你沒有任何正在做這件事的CSS嗎? – BalusC 2009-12-20 15:29:30

+0

@BalusC:任何東西都可以用IE :) – 2009-12-20 15:44:18

+0

@Andreas:我在IE6/7/8中測試了這個代碼片段,它帶有兩個不同的透明PNG圖片,並且它在所有工作中都很好(除了aplha通道不存在於IE6,但這是另一個問題)。 – BalusC 2009-12-20 19:54:05

回答

1

您可以嘗試$(this).after();並在想要替換的圖片後添加新圖片;最後做$(this).remove();

+0

這有效,但切換圖像時會導致閃爍。 – jack 2009-12-21 01:01:29