Home > Web Front-end > JS Tutorial > Summary of parentNode in DOM

Summary of parentNode in DOM

一个新手
Release: 2017-10-09 09:39:26
Original
1381 people have browsed it

<!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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template