您好,欢迎访问一九零五行业门户网

让FireFox支持innerText的实现代码_javascript技巧

为firefox实现innertext属性
很多代码写了又忘忘了又写,很浪费,所以决定养成做笔记的习惯。
知识点:
0、为什么要innertext?因为安全问题
1、为firefox dom模型扩展属性
2、currentstyle属性可以取得实际的style状态
3、ie实现innertext时考虑了display方式,如果是block则加换行
4、为什么不用textcontent?因为textcontent没有考虑元素的display方式,所以不完全与ie兼容
复制代码 代码如下:
cccdddeeee
fff
今天在制作firefox下支持复制的js代码的时候,用到了innertext,测试发现原来firefox支持innerhtml但不支持innertext,所以上网找了一下,发现了一篇非常不错的代码。另从回复中,我们得到了如下兼容代码。修正了原来ie下出现错误提示的问题。具体的看下么的文章。
把这段加在你所js文件中就可以在mozilla/firefox下使用innertext
复制代码 代码如下:
htmlelement.prototype.__definegetter__
(
innertext,
function ()
{
var anystring = ;
var childs = this.childnodes;
for(var i=0; i{
if(childs[i].nodetype==1)
anystring += childs[i].tagname==br ? '\n' : childs[i].innertext;
else if(childs[i].nodetype==3)
anystring += childs[i].nodevalue;
}
return anystring;
}
);
但这段代码在ie中它会提示htmlelement未定义,下面就是具体的解决方法。
复制代码 代码如下:
function isie(){ //ie? 判断是不是ie
if (window.navigator.useragent.indexof(msie)>=1)
return true;
else
return false;
}
if(!isie()){
htmlelement.prototype.__definegetter__
(
innertext,
function ()
{
var anystring = ;
var childs = this.childnodes;
for(var i=0; i{
if(childs[i].nodetype==1)
anystring += childs[i].tagname==br ? '\n' : childs[i].innertext;
else if(childs[i].nodetype==3)
anystring += childs[i].nodevalue;
}
return anystring;
}
);
}
其它类似信息

推荐信息