×

JS 浏览器兼容

FireFox支持innerText

天外来信 天外来信 发表于2012-07-07 01:23:27 浏览2948 评论0

抢沙发发表评论

默认FireFox是不支持innerText的,不过它有一个属性textContent的作用和innerText是一样的,使用方法如下:

 

Js代码  收藏代码
  1. document.write(document.body.textContent);  

 

对于习惯使用innerText的人来说有点不舒服,于是网上有人就给FireFox也创建了一个innerText属性,代码如下:

 

Js代码  收藏代码
  1. <script language="javascript">  
  2. function isIE(){ //ie?   
  3. if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1)   
  4.     return true;   
  5. else   
  6.     return false;   
  7. }   
  8.   
  9. if(!isIE()){ //firefox innerText define  
  10.     HTMLElement.prototype.__defineGetter__("innerText",   
  11.     function(){  
  12.         var anyString = "";  
  13.         var childS = this.childNodes;  
  14.         for(var i=0; i<childS.length; i++) {   
  15. Webjx.Com  
  16.   
  17.   
  18.             if(childS[i].nodeType==1)  
  19.                 //anyString += childS[i].tagName=="BR" ? "\n" : childS[i].innerText;  
  20.                 anyString += childS[i].innerText;  
  21.             else if(childS[i].nodeType==3)  
  22.                 anyString += childS[i].nodeValue;  
  23.         }  
  24.         return anyString;  
  25.     }   
  26.     );   
  27.     HTMLElement.prototype.__defineSetter__("innerText",   
  28.     function(sText){  
  29.         this.textContent=sText;   
  30. Webjx.Com  
  31.   
  32.     }   
  33.     );   
  34. }  
  35. </script>  

评论列表

访客