Home  >  Article  >  Web Front-end  >  Sibling nodes in DOM script programming_javascript skills

Sibling nodes in DOM script programming_javascript skills

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

Browsers other than IE use line breaks as text nodes (nodeType is 3). For elements, nodeType is 1. Here's a practical way to find them:

Copy the code The code is as follows:

lastSibling:function( node){
var tempObj = node.parentNode.lastChild;
while(tempObj.nodeType!=1 && tempObj.previousSibling!=null)
{
tempObj=tempObj.previousSibling;
}
return (tempObj.nodeType==1)?tempObj:false;
}

This is the source code of the lastSibling method in the DOMhelp library in the book "JavaScript in a Simple Language". It is similar to the source code implemented in the mootools library:
Copy the code The code is as follows:

'last-child ': function(){
var element = this;
while ((element = element.nextSibling)){
if (element.nodeType == 1) return false;
}
return true;
}

This is the last-child() method in the Mootools 1.2.4 source code.
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