javascript让firefox支持innerText

来源:互联网 发布:update例子 mysql 编辑:程序博客网 时间:2024/05/25 23:25

 

  1.  <script type="text/javascript">
  2. function test() {
  3. alert(document.getElementById("div1").innerText);
  4. }
  5. </script>
  6. <div id="div1">Hello world!</div>
  7. <input type="button" value="OK" onclick="test();" />

在IE下,正常弹出“Hello world!”;而在firefox中,弹出的是“undefined”的未定义错误;

现在用javascript编写这样的一段代码

  1. try
  2. {
  3.    HTMLElement.prototype.__defineGetter__ ("innerText"function ()
  4.    {
  5.       var anyString = "";
  6.       var childS = this.childNodes;
  7.       for(var i = 0; i < childS.length; i ++ )
  8.       {
  9.          if(childS[i].nodeType == 1)anyString += childS[i].tagName == "BR" ? '"n' : childS[i].innerText;
  10.          else if(childS[i].nodeType == 3)anyString += childS[i].nodeValue;
  11.       }
  12.       return anyString;
  13.    }
  14.    );
  15. }
  16. catch(e)
  17. {
  18. }
原创粉丝点击