firefox浏览器不支持innerText的解决方法
更新时间:2013年08月07日 15:25:25 作者:
在测试时发现firefox不支持innerText,该怎么办呢?其实很简单,本文为大家提供了一个解决方法,感兴趣的朋友可以参考下,希望对大家有所帮助
js代码:
<script>
window.onload = function(){
<PRE class=javascript name="code">if(window.navigator.userAgent.toLowerCase().indexOf("msie")==0){ //firefox innerText
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;
}
);
};</PRE>var test = document.getElementById("test");<BR>
var innerText_s = test.innerText;<BR>
if( innerText_s == undefined ){<BR>
alert( test.textContent ); // firefox<BR>
}else{ <BR>
alert( test.innerText);<BR>
};<BR>
<BR>
<BR>
}<BR>
<BR>
<BR>
</script><BR>
<PRE></PRE>
<P><BR>
</P>
<P>html代码</P>
<P><div id="test"><BR>
<span style="color:red">test1</span> test2<BR>
</div><BR>
</P>
复制代码 代码如下:
<script>
window.onload = function(){
<PRE class=javascript name="code">if(window.navigator.userAgent.toLowerCase().indexOf("msie")==0){ //firefox innerText
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;
}
);
};</PRE>var test = document.getElementById("test");<BR>
var innerText_s = test.innerText;<BR>
if( innerText_s == undefined ){<BR>
alert( test.textContent ); // firefox<BR>
}else{ <BR>
alert( test.innerText);<BR>
};<BR>
<BR>
<BR>
}<BR>
<BR>
<BR>
</script><BR>
<PRE></PRE>
<P><BR>
</P>
<P>html代码</P>
<P><div id="test"><BR>
<span style="color:red">test1</span> test2<BR>
</div><BR>
</P>
相关文章
一种新的javascript对象创建方式Object.create()
这篇文章主要介绍了一种新的javascript对象创建方式Object.create(),感兴趣的小伙伴们可以参考一下2015-12-12
《javascript设计模式》学习笔记四:Javascript面向对象程序设计链式调用实例分析
这篇文章主要介绍了Javascript面向对象程序设计链式调用,结合实例形式分析了《javascript设计模式》中链式调用的原理与简单使用技巧,需要的朋友可以参考下2020-04-04
js完美解决IE6不支持position:fixed的bug
关于IE6,虽然它已被微软抛弃很久了,但是由于大天朝的特殊行情(盗版)对于前端工程师来说,解决IE6兼容position:fixed的问题显得很重要。特别是你需要用到头尾悬停调用的时候2015-04-04
JavaScript 使用 splice 方法删除数组元素可能导致的问题分析
这篇文章主要介绍了JavaScript 使用 splice 方法删除数组元素可能导致的问题分析,当在 JavaScript 中从数组中删除元素时,使用 splice 方法时需要谨慎,本文给大家详细讲解,需要的朋友可以参考下2023-04-04


最新评论