本文 7570 pv

0

让IE与FF浏览器支持png格式的图片

© kekehu / 技术资源 / 2008.10.30 / 10:46 / 7570PV

    今天突然发现公司的一个专题页面在FF浏览器上有报错,在网上查了一下,他用了IE支持PNG的代码,window.attachEvent("onload", correctPNG)这个代码在FF上说不是一个function,我查了一下发现FF不支持attachEvent,而是对addEventListener支持,最后加一个函数来断定浏览器调用。代码如下:


<script language="JavaScript" type="text/JavaScript">
//<![CDATA[
function correctPNG()
   {
   for(var i=0; i<document.images.length; i++)
      {
    var img = document.images[i]
    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 = "<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
     i = i-1
       }
      }
   }
  
   //addEventListener与attachEvent
   function addEvent(elm, evType, fn, useCapture)
   {
  if (elm.addEventListener)
  {
   elm.addEventListener(evType, fn, useCapture);
   return true;
  }
  else if (elm.attachEvent)
  {
   var r = elm.attachEvent('on' + evType, fn);
   return r;
  }
  else
  {
   elm['on' + evType] = fn;
  }
   }

addEvent(window,'load',correctPNG,false);
//window.attachEvent("onload", correctPNG); 这个只持IE
//]]>
</script>

本文有 0 篇评论

发表你的见解

打开HTML 打开UBB 打开表情 隐藏 记住我
emotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemot
emotemotemotemotemot