木子屋 Dnawo's BLOG

让FireFox支持innerText

👤 dnawo 📅 2008-02-01 👁 4794 👍 0 💬 0 🔄 本站原创
默认FireFox是不支持innerText的,不过它有一个属性textContent的作用和innerText是一样的,使用方法如下:

document.write(document.body.textContent);

对于习惯使用innerText的人来说有点不舒服,于是网上有人就给FireFox也创建了一个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].innerText;
				anyString += childS[i].innerText;
			else if(childS[i].nodeType==3)
				anyString += childS[i].nodeValue;
		}
		return anyString;
	} 
	); 
	HTMLElement.prototype.__defineSetter__("innerText", 
	function(sText){
		this.textContent=sText; 
	} 
	); 
}
</script>

评论(0)

暂无评论。

计算题
评论需审核通过后显示
← 上一篇 创建Wap服务器 下一篇 → CSS常用技巧总结