innerText引发的错误

   

  因为firefox对innerText的不支持,所以以下代码在firefox里运行有错误。  

       //重新加载饼图
            if ($("#hidden_strTitle").val() != "2") {
                var gridView = document.getElementById("GridViewlb");
                var sum1 = gridView.rows[rowIndex + 1].cells[1].innerText;
                var sum2 = gridView.rows[rowIndex + 1].cells[2].innerText;
                var sum3 = gridView.rows[rowIndex + 1].cells[3].innerText;
                var sum4 = gridView.rows[rowIndex + 1].cells[4].innerText;
                var sum5 = gridView.rows[rowIndex + 1].cells[5].innerText;
                var sum6 = gridView.rows[rowIndex + 1].cells[6].innerText;
                createPieChart(sum1, sum2, sum3, sum4, sum5, sum6);
            }

  为了让上段代码在firefox里运行也正确,需要对innerText作一定的处理,textContent在一程度上可以代替innerText,但contentText有个缺点:文本中间的空白符会被无情的抹去,所以将文本间的空白符保留下来。为innerText适应firexfox,只需在页面中加入如下代码:

<script type="text/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" ? '
' : childS[i].innerText;
                        }
                        else if (childS[i].nodeType == 3) {
                            anyString += childS[i].nodeValue;
                        }
                    }
                    return anyString;
                }
              );
              HTMLElement.prototype.__defineSetter__("innerText",
                function(sText){
                  this.textContent=sText;
                }
              );
        }
    </script>

  结果,程序能正确运行了。

  

  

  这里有篇文章关于firefox与IE对javascript的异同

原文地址:https://www.cnblogs.com/danshui/p/3275870.html