除ie外的浏览器是将换行符作为内容的文本节点(nodetype为3)。而元素的话,nodetype为1。下面是查找它们的实用方法:
复制代码 代码如下:
lastsibling:function(node){
var tempobj = node.parentnode.lastchild;
while(tempobj.nodetype!=1 && tempobj.previoussibling!=null)
{
tempobj=tempobj.previoussibling;
}
return (tempobj.nodetype==1)?tempobj:false;
}
这是《深入浅出javascript》书中domhelp库中lastsibling方法的源码。与 mootools 库中实现源码差不多:
复制代码 代码如下:
'last-child': function(){
var element = this;
while ((element = element.nextsibling)){
if (element.nodetype == 1) return false;
}
return true;
}
这是在 mootools 1.2.4 源码中的 last-child() 方法。