Traverse the node tree and calendar the node tree
The information read from the following attributes allows us to understand the relationship between adjacent nodes.
- The childNodes attribute returns an array consisting of subnodes of the given element node:
nodeList = node.childNodes
The returned array is a nodeList set. Each node in this nodeList set is a Node object. These node objects all have common node attributes such as nodeType, nodeName, and nodeValue.
Neither the text node nor the attribute node can contain any subnode, so their childNodes attribute always returns an empty array.
If you only want to know whether an element has subnodes, you can use the hasChildNodes method.
To know how many nodes A element has, use the length attribute of the childNodes array:
node.childNodes.length
Even if the element has only one subnode, The childNodes attribute returns a node array instead of a single node. In this case, the length value of the childNodes array is 1. For example, if the document element on a webpage only contains the child node of the html element, the document. childNodes [0]. the nodeName value is HTML.
The childNodes attribute is a read-only attribute. To add a subnode to an element, you can use the appendChild () or insertBefore () method. to delete an element node, you can use removeChild () method. When you use these methods to subtract a child node of an element, the childNodes attribute of this element is automatically refreshed.
reference = node.firstChild
This property returns a reference pointer to a Node object, which has common attributes such as nodeType, nodeName, and nodeValue.
Text nodes and attribute nodes cannot contain any subnodes, so their firstChild attribute will always return null.
FirstChild is a read-only attribute.
LastChild: The property returns the last child node of a given element:
reference = node.lastChild
This property returns a reference pointer to a node to bear. This Node object has common node attributes such as nodeType, nodeName, and nodeValue.
Neither the text node attribute nor the property node attribute can contain any subnode. Therefore, the lastChild attribute always returns null.
The lastChild attribute of an element is equivalent to the last node in the childNodes node set:
refrence = node.childNodes[element.childNodes.lelgth-1]
If you want to know whether an element has subnodes, you can use the hasChildNodes method. If a node does not have any subnodes, its lastChild attribute returns null.
The lastChild attribute is a read-only attribute.
The nextSibing attribute returns the next subnode of a given node:
reference = node.nextSibing
This property returns a reference pointer to a Node object, which has common attributes such as nodeType, nodeName, and nodeValue.
If no node belongs to the same parent node after a given node, its nextSibling attribute returns null.
The nextSibling attribute is a read-only attribute.
The parentNode attribute returns the parent node of a given node:
reference = node.parentNode
This property returns a reference pointer to a Node object, which has common node attributes such as nodeType, nodeName, and nodeValue.
The node returned by the parentNode attribute is always an element node, because only an element node can contain subnodes. The only exception is the document node, which has no parent node. In other words, the parentNode attribute of the document node returns null.
The parentNode attribute is a read-only attribute.
reference = node.previousSibling
This attribute returns the reference pointer of the previous Node object, which has common node attributes such as nodeType, nodeName, and nodeValue.
If no previous Element Node exists, null is returned.
The previussibling attribute is a read-only attribute.