手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >让innerText在firefox火狐和IE浏览器都能用的写法
让innerText在firefox火狐和IE浏览器都能用的写法
摘要:IE中的获取文本方法innerText在firefox中不支持firefox改成了textContent方法/属性并且在Firefox中文本...

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>

【让innerText在firefox火狐和IE浏览器都能用的写法】相关文章:

png在IE6 下无法透明的解决方法汇总

让iframe框架网页在任何浏览器下自动伸缩

模拟抽奖

文本框栏目介绍

10条建议帮助你创建更好的jQuery插件

javascript中FOREACH数组方法使用示例

JavaScript实现单击下拉框选择直接跳转页面的方法

讲解JavaScript中for...in语句的使用方法

如何实现浏览器上的右键菜单

解决未知尺寸的图片撑破页面的问题

精品推荐
分类导航