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 = "<span " + imgID + imgClass + imgTitle
+ " style="" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src='" + img.src + "', sizingMethod='scale');"></span>"
img.outerHTML = strNewHTML;
j = j - 1;
}
}
}
}
//判断是否为IE8及以下浏览器,其实除了这三个浏览器不支持addEventListener,其它浏览器都没问题
if (typeof window.addEventListener == "undefined" && typeof document.getElementsByClassName == "undefined") {
window.attachEvent("onload", correctPNG);
}
</script>
在页面的/body的结束标记之前先引用jquery1.8类库,再加入以上代码,IE6 7 8 显示PNG24都没有问题了,如果需要执行animate动画或获取图片时,发现在ie 6 7 8下找不到PNG图片了,或找到了改变其位置和透明图没有反应,这原因是correctPNG将页面上所有的PNG的IMG标签都替换成了SPAN标签,然后在SPAN标签上使用filter : progid:DXImageTransform.Microsoft.AlphaImageLoader 将PNG图片加载进来,所以,建议做法是在将图片用DIV包括起来,此DIV中只允许有一个IMG标签,然后对DIV进行位置或透明度的相关操作,例:
<div id='test'><img src='xxxx.png'/></div>
<script>
$("#test").animate({opacity:0.2,marginLeft:500},1000,function(){alert('run complete');});
</script>还有一种情况是,我对这个图片除了要做透明和位移,还要改变其宽度和高度,对于这种情况,我建议采用以下方法
<div id='test'><img src='xxxx.png'/></div>
<script>
$($("#test span")[0]||$("#test img")[0]).animate({opacity:0.2,marginLeft:500,width:'500px',height:'500px'},1000,function(){alert('run complete');});
</script>
有问题欢迎共同探讨










