<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>parentNode</title> <script type="text/javascript"> "use strict" window.onload=function(){ var oUl=document.getElementById("ul1"); var nodeList=oUl.childNodes; // console.log(nodeList); var arr=convertToArray(nodeList,0); console.log(arr); for(var i=0,len=arr.length;i<len;i++){ console.log(arr[0].parentNode); } console.log(arr[0].nextSibling); //第一个子节点是没有previousSibling的 console.log(arr[0].previousSibling); } function convertToArray(nodes){ var array=null; try{ array=Array.prototype.slice.call(nodes,0); }catch(ex){ array=new Array(); for(var i=0,len=nodes.length;i<len;i++){ array.push(nodes[i]); } } return array; } </script> </head> <body> <ul id="ul1"> <li>111</li> <li>222</li> <li>333</li> </ul> </body> </html>
All parentNodes of the child nodes of a certain parent node point to the same node. The above code is typed in a loop, as follows:
In addition, each child node will have nextSibling and preciousSibling. Of course, If it is the first child node, there is no previousSibling, and the last child node has no nextSibling. The returned results are null
There will be firstChild and lastChild in the set of each child node. Only when there are no children In the case of nodes, both values must be null to be equal.
The above is the detailed content of Summary of parentNode in DOM. For more information, please follow other related articles on the PHP Chinese website!