让innerText在firefox火狐和IE浏览器都能用的写法

IE中的获取文本方法innerText在firefox中不支持
firefox改成了textContent方法/属性

并且在Firefox中文本中间的空白自符被无情的替换没了
使用起来异常不方便
现在好了,用Javascript重新定义了innerText方法
使得在Firefox中也可以使用innerText方法
并且此方法解决了firefox中空白字符的问题

使用方法:
将下面的脚本放在页面内
不管ie还是firefox都可以使用obj.innerText提取文本了

 1 <script language=”javascript”> 
 2 function isIE(){ //ie? 
 3 if (window.navigator.userAgent.toLowerCase().indexOf(“msie”)>=1) 
 4 return true; 
 5 else 
 6 return false; 
 7 } 
 8 if(!isIE()){ //firefox innerText define 
 9 HTMLElement.prototype.__defineGetter__( “innerText”, 
10 function(){ 
11 var anyString = “”; 
12 var childS = this.childNodes; 
13 for(var i=0; i<childS.length; i++) { 
14 if(childS[i].nodeType==1) 
15 anyString += childS[i].tagName==”BR” ? ‘\n' : childS[i].textContent; 
16 else if(childS[i].nodeType==3) 
17 anyString += childS[i].nodeValue; 
18 } 
19 return anyString; 
20 } 
21 ); 
22 HTMLElement.prototype.__defineSetter__( “innerText”, 
23 function(sText){ 
24 this.textContent=sText; 
25 } 
26 ); 
27 } 
28 </script> 

 

Getter是一种获取一个属性的值的方法,Setter是一种设置一个属性的值的方法。可以为任何预定义的核心对象或用户自定义对象定义getter和setter方法,从而为现有的对象添加新的属性。
有两种方法来定义Getter或Setter方法:

在对象初始化时定义
在对象定义后通过Object的__defineGetter__、__defineSetter__方法来追加定义
在使用对象初始化过程来定义Getter和Setter方法时唯一要做的事情就是在getter方法前面加上“get”,在setter方法前面加上“set”。
还有一点要注意的就是getter方法没有参数,setter方法必须有一个参数,也就是要设置的属性的新值。
例如:

复制代码 代码如下:
o = {
value:9,
get b() {return this.value;},
set setter(x) {this.value = x;}
}

在对象定义后给对象添加getter或setter方法要通过两个特殊的方法__defineGetter__和__defineSetter__。这两个函数要求第一个是getter或setter的名称,以string给出,第二个参数是作为getter或setter的函数。
例如我们给Date对象添加一个year属性:

复制代码 代码如下:
Date.prototype.__defineGetter__('year', function() {return this.getFullYear();});
Date.prototype.__defineSetter__('year', function(y) {this.setFullYear(y)});

var now = new Date;
alert(now.year);
now.year = 2006;
alert(now);

至于采用哪种形式主要取决于个人的编程风格,采用第一种形式结构紧凑,更容易理解。但是假如你想在对象定义以后再添加Getter或Setter,或者这个对象的原型不是你写的或是内置对象,那么只好采用第二种方式了。
下面是一个为Mozilla浏览器添加innerText属性的实现:

复制代码 代码如下:
HTMLElement.prototype.__defineGetter__
(
"innerText",function()
//define a getter method to get the value of innerText,
//so you can read it now!
{
var textRange = this.ownerDocument.createRange();
//Using range to retrieve the content of the object
textRange.selectNodeContents(this);
//only get the content of the object node
return textRange.toString();
// give innerText the value of the node content
}
);

详细出处参考:http://www.jb51.net/article/9913.htm

posted @ 2013-01-23 14:20  ggzjj  阅读(302)  评论(0编辑  收藏  举报