jquery 版的 ie 下 png 图片透明的 js 函数

1 function correctPNG(objTag) {

2 // 为 IE 时生效

3 if(jQuery.browser.msie) {

4 $(objTag).each(function(){

5 var src = $(this).attr(“src”);

6 // src 不为空,且为 png 格式图片

7 if (src != undefined && src.substring(src.length – 3, src.length).toLowerCase() == “png”) {

8 // 取出图片原有属性

9 var imgId = $(this).attr(“id”);

10 imgId = imgId == undefined ? : imgId;

11 var imgClass = $(this).attr(“class”);

12 imgClass = imgClass == undefined ? : imgClass;

13 var imgTitle = $(this).attr(“title”);

14 imgTitle = imgTitle == undefined ? : imgTitle;

15 var imgStyle = $(this).attr(“style”);

16 imgStyle = imgStyle == undefined ? : imgStyle;

17 imgStyle = “display:inline-block;” + imgStyle;

18 var imgAlign = $(this).attr(“align”);

19 // 图片对齐属性转为样式

20 switch(imgAlign) {

21 case “left”:

22 imgStyle = “float:left;” + imgStyle;

23 break;

24 case “right”:

25 imgStyle = “float:right;” + imgStyle;

26 break;

27 }

28 // 父标签为 a 时,应用鼠标样式为手指

29 if ($(this).parent().get(0).tagName.toLowerCase() == “a”) {

30 imgStyle = “cursor:hand;” + imgStyle;

31 }

32 // 设置宽高

33 imgStyle = “width:” + $(this).width() + “px; height:” + $(this).height() + “px;” + imgStyle;

34 // 设置 IE 的 PNG 透明滤镜

35 imgStyle = “filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\” + src + \”,sizingMethod=\”scale\”);” + imgStyle;

36 // 替换 img 标签为 span

37 $(this).replaceWith(“<span id='” + imgId + “‘ class='” + imgClass + “‘ title='” + imgTitle + “‘ style='” + imgStyle + “‘></span>”);

38 }

39 });

40 }

41 }

Leave a comment

Your email address will not be published. Required fields are marked *