简体   繁体   中英

What are parent and child nodes in practical use in html?

I'm really lost on this concept. I've read up on the DOM and the definitions of nodes but I still don't really get it in a practical sense. For instance,

<div>
  <p></p>
</div>

Would <div> be the parent and <p> be the child?

Exactly, in your code <div> would be the parent and <p> the child. Here is a piece of code

<html>
<title>example</title>

<body>

<head>

    <div> </div>

</head>

</body>
</html>

In the code above <div> is the child of <head> , <head> child of <body> and <body> child of <html> . Starting from <html> parent of <body> , <body> parent of <head> and so on.

Yes, <div> is the parent and <p> is the child

To understand it a bit more let's add another <p>

<div>
    <p id='first-paragraph'></p>
    <p id='second-paragraph'></p>
</div>

Now <p id='first-paragraph'> and <p id='second-paragraph'> are both children of <div>

And one more fact is that they are siblings, since they are on the same level sharing the same parent ( <div> )

the html structure is same like tree structure first is root that is html and elements under direct is its child and this goes on nested with elements add under this order the image may give u clear picture 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM