Nodes and node trees
The concept of nodes and node trees
Each item in an HTML document is a node, including HTML tags, tag attributes, text content, comments, spaces or tabs, etc.
All nodes in the HTML document form a node tree (or document tree). Each element, attribute, text, etc. in an HTML document represents a node in the tree. The tree starts at the document node and continues branching out from there to all text nodes at the lowest level of the tree.
Relationship between nodes
There are hierarchical relationships between DOM nodes, including parent nodes, child nodes, sibling nodes (peer nodes), descendants, parents, etc.
DOM节点之间的关系 这是标题
这是内容
As can be seen from the above code:
Every node except the document node (root node) has a parent node.
For example, the parent node of
node.
Most element nodes have child nodes.
For example, the
When nodes have a common parent node, they are siblings (sibling nodes).
For example,
and
are sibling nodes, and their parent nodes are both
Nodes can also have descendants, which refer to all the child nodes of a node, or the child nodes of these child nodes, and so on.
For example, all text nodes are descendants of the node, and the first text node is a descendant of the
Nodes can also have ancestors. An ancestor is the parent node of a node, or the parent node of a parent node, and so on.
For example, all text nodes can have the node as an ancestor node.