---
title: "修复 IE 下 PNG 图片不能透明显示的问题 "
author: "LD_21"
date: "2010-06-21T13:52:15Z"
tags: ["UI"]
categories: ["Web前端"]
slug: "ie-png"
draft: false
lastmod: "2010-06-21T13:56:12Z"
id: 695602
url: "https://ld-21.iteye.com/blog/695602"
---

// 修复 IE 下 PNG 图片不能透明显示的问题  
&lt;script type="text/javascript"&gt;

function correctPNG(){ // correctly handle PNG transparency in Win IE 5.5 & 6.   
     var arVersion = navigator.appVersion.split("MSIE");  
     var version = parseFloat(arVersion\[1]);  
     if ((version &gt;= 5.5) && (document.body.filters)) {  
      for(var j=0; j&lt;document.images.length; j++){  
       var img = document.images\[j];  
       var imgName = img.src.toUpperCase();  
       if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){  
        var imgID = (img.id) ? "id='" + img.id + "' " : "";  
        var imgClass = (img.className) ? "class='" + img.className + "' " : "";  
        var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";  
        var imgStyle = "display:inline-block;" + img.style.cssText;  
        if (img.align == "left") imgStyle = "float:left;" + imgStyle;  
        if (img.align == "right") imgStyle = "float:right;" + imgStyle;  
        if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;  
        var strNewHTML = "&lt;span " + imgID + imgClass + imgTitle  
         + " style=\\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"  
         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"  
         + "(src=\\'" + img.src + "\\', sizingMethod='scale');\\"&gt;&lt;/span&gt;" ;  
        img.outerHTML = strNewHTML;  
        j = j-1;  
       }  
      }  
     }     
}  
window.attachEvent("onload", correctPNG);

&lt;/script&gt;

 

使用时：先引入上面的JS 然后在使用的地方：

&lt;img src="images/logo.png" alt="http://glhaohao.com/ - 桂林好又好教育咨询有限公司" onload="return correctPNG();" /&gt;