JavaScript pare...LOGIN

JavaScript parentNode: Get the parent node

Syntax:
nodeObject.parentNode
Among them, nodeObject is the node object (element node).

For example, get the parent node of the node with id="demo":

document.getElementById("demo").parentNode;

For example, get the id="demo" "The parent node of the node:

<div>
    <div id="demo">点击这里获取父节点</div>
</div>
<script type="text/javascript">
    document.getElementById("demo").onclick=function(){
        var demoParent=this.parentNode;
        alert(
            "父节点的名称是:"+demoParent.nodeName+"\n"+
            "父节点的类型是:"+demoParent.nodeType
        );
    }
   
</script>

Please see the following demonstration:

QQ截图20161013093529.png

Next Section
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>HTML DOM树型结构图</title> <div> <div id="demo">点击这里获取父节点</div> </div> <script type="text/javascript"> document.getElementById("demo").onclick=function(){ var demoParent=this.parentNode; alert( "父节点的名称是:"+demoParent.nodeName+"\n"+ "父节点的类型是:"+demoParent.nodeType ); } </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware