火狐FF和IE兼容下innerText的问题处理

来源:互联网 发布:对网络暴力的看法 编辑:程序博客网 时间:2024/05/02 20:46

第一种方式:

把上述JS放入jsp页面。

 

<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"
                     ? "/r/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>

第二种方式:

if(navigator.appName.indexOf("Explorer") > -1)采用if判断,火狐的就用textContent可以获取。

 

在火狐中会出现innerText空白,处理方法:obj.innerText.replace(/(^/s*)|(/s*$)/g,"")

原创粉丝点击