1、问题描述
在IE和Opear下,DOM对象支持innerText属性,可以很方便的取得HTML标签内的值。但是Firefox不支持该属性,好在FireFox下的DOM对象支持textContent,该属性与innerText等效。
2、解决方案
为HTMLElement添加innerText属性,实现与在IE下同样的功用。
3、参考
示例代码:
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].innerText;
else if(childS[i].nodeType==3)
anyString += childS[i].nodeValue;
}
return anyString;
});
HTMLElement.prototype.__defineSetter__( "innerText", function(sText){
this.textContent=sText;
});