首页 HTML/CSS span的innerText属性ff火狐浏览器兼容性问题解决方案

span的innerText属性ff火狐浏览器兼容性问题解决方案

作者:胡同里的砖头 围观群众:1825 更新于:2013-12-19

IE中的获取文本方法innerText在firefox中不支持
firefox改成了textContent方法/属性
并且在Firefox中文本中间的空白自符被无情的替换没了
使用起来异常不方便
现在好了,用Javascript重新定义了innerText方法
使得在Firefox中也可以使用innerText方法
并且此方法解决了firefox中空白字符的问题

使用方法:
将下面的脚本放在页面内
不管ie还是firefox都可以使用obj.innerText提取文本了

<script language="javascript">
function isIE(){ //ie?
   if (window.navigator.userAgent.toLowerCase().indexOf("msie")>=1)
    return true;
   else
    return false;
}

if(!isIE()){ //firefox innerText define
   HTMLElement.prototype.__defineGetter__(     "innerText",
    function(){
     var anyString = "";
     var childS = this.childNodes;
     for(var i=0; i<childS.length; i++) {
      if(childS[i].nodeType==1)
       anyString += childS[i].tagName=="BR" ? '\n' : childS[i].textContent;
      else if(childS[i].nodeType==3)
       anyString += childS[i].nodeValue;
     }
     return anyString;
    }
   );
   HTMLElement.prototype.__defineSetter__(     "innerText",
    function(sText){
     this.textContent=sText;
    }
   );
}
</script>

  • 本文标题: span的innerText属性ff火狐浏览器兼容性问题解决方案
  • 文章分类:【HTML/CSS】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8