Traverse the node tree and calendar the node tree

Source: Internet
Author: User

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.

  • FirstChild
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

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.

  • NextSibing

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.

  • ParentNode

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.

  • Previussibling
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.