低版本Firefox支持innerText属性兼容方法

来源:互联网 发布:java乱码转换成中文 编辑:程序博客网 时间:2024/06/03 20:09

FireFox支持innerText属性了,很遗憾是44.0.2版还需要兼容处理

innerHTML是符合W3C标准的属性,而innerText只适用于IE浏览器,因此,尽可能地去使用innerHTML,而少用innerText,如果要输出不含HTML标签的内容,可以使用innerHTML取得包含HTML标签的内容后,再用正则表达式去除HTML标签或者用innerText更便捷。

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>innerText</title></head><body><div id="hello">this is a paragraph <p>hello world</p></div><script>if(!('innerText' in document.body)){ /* 一个不是w3c标准的属性 能展现节点及子节点的文本 FireFox 45以下版本不支持, 45及之后的版本都支持了*/HTMLElement.prototype.__defineGetter__("innerText", function(){return this.textContent;});HTMLElement.prototype.__defineSetter__("innerText", function(s){return this.textContent = s;});}   var p = document.getElementById('hello');   console.log(p.innerText);   p.innerText = "the paragraph has changed!";   console.log(p.innerText);</script></body></html>


FireFox输出:
this is a paragraph hello world
the paragraph has changed!
Chrome输出:
this is a paragraph
hello world
the paragraph has changed!

0 0
原创粉丝点击