Home  >  Article  >  Web Front-end  >  DOM 脚本编程中的兄弟节点_javascript技巧

DOM 脚本编程中的兄弟节点_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:42:531231browse

除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() 方法。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn