nodeValue innerTexte在操作存在&nbsp的节点的区别

<table id="testtable">
<tbody>
<tr>
<td>
<div style="width:10px;height:10px;border:1px solid black"">&nbsp;</div>
</td>
<td>
<div style="width:10px;height:10px;border:1px solid black">&nbsp;</div>
</td>
</tr>
<tr>
<td>
<div style="width:10px;height:10px;border:1px solid black">&nbsp;</div>
</td>
<td>
<div style="width:10px;height:10px;border:1px solid black">&nbsp;</div>
</td>
</tr>
</tbody>
</table>

IE下发现

jquery 1.3  $("div").text().length>0  text()使用nodeValue取值

jquery 1.7 $("div").text().length=0  text()使用innerText取值


$("#testtable tr td").click(function(){

$div=$(this).find("div");
<!--在IE下 对于&nbsp nodeValue与innerText输出结果不同   FireFox不支持innerText-->
$div.each(function(){
var nt=this.childNodes[0].nodeType;
var nv=this.childNodes[0].nodeValue;

alert("使用1.7.2 $.trim----"+$.trim(nv).length);<!--输出0 -->

 alert("使用1.3 $.trim----"+$.trim(nv).length);<!--输出1 -->

alert(nv.replace(/^\s+|\s+$/g, "").length);alert(/^\s+|\s+$/g.test(nv));<!--IE下false  FF下true -->

if(typeof this.innerText!="undefined"){ 

alert(this.innerText.replace(/^\s+|\s+$/g, "").length);

alert(/^\s+|\s+$/g.test(this.innerText));

}
});



<!--IE doesn't match non-breaking spaces with \s-->

rtrim = /\S/.test("\xA0") ? (/^[\s\xA0]+|[\s\xA0]+$/g) : /^\s+|\s+$/g,      //'\xA0'为&nbsp的ascii扩展码 http://www.ascii-code.com/

下面是1.7版本对$.trim()的解释,用的就是上面rtrim的正则表达式
<!--The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. 
If these whitespace characters occur in the middle of the string, they are preserved.-->


<!--早期版本的$.trim() 不支持non-breaking spaces
jquery 1.3.2
trim : function(E) {
return (E || "").replace(/^\s+|\s+$/g, "")
},
-->

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值