Traversing a node tree

Source: Internet
Author: User

The information read from the following properties allows us to understand the relationship between neighboring nodes.

    • The ChildNodes property returns an array that consists of the child nodes of the given element node:
NodeList = Node.childnodes

The array returned by this property is a NodeList collection. Each node in the NodeList collection is a node object. These node objects have common node properties such as NodeType, NodeName, NodeValue, and so on.

Neither the text node nor the attribute node can contain any child nodes, so their ChildNodes property always returns an empty array.

If you just want to know if an element has child nodes, you can use the HasChildNodes method.

If you want to know how many nodes an element has, use the length property of the ChildNodes array:

Node.childNodes.length

Even if the element has only one child node, the ChildNodes property returns an array of nodes instead of returning a single node, at which time the length value of the childnodes array will be 1, for example, If the document element on a webpage has only the child node of the HTML element, then the Document.childnodes[0].nodename value will be HTML.

The ChildNodes property is a read-only property and you can use the appendchild () or InsertBefore () method if you need to add child nodes to an element, or you can use the RemoveChild () method if you need to delete an element node This element ChildNodes property is also automatically refreshed when you use these methods to once, minus a child node of an element.

    • FirstChild
Reference = Node.firstchild

This property returns a reference pointer to a node object that has common properties such as NodeType, NodeName, NodeValue, and so on.

Text nodes and attribute nodes are not likely to contain any child nodes, so their FirstChild property will always return null.

FirstChild is a read-only property.

    • LastChild

LastChild: The property returns the last child node of a given element:

Reference = Node.lastchild

This property returns a node-to-bear reference pointer, which has common node attributes such as NodeType, NodeName, NodeValue, and so on.

Neither text node properties nor attribute node properties can contain any child nodes, so the LastChild property always returns NULL.

The LastChild property of an element is equivalent to the last node of the element childnodes node collection:

refrence = Node.childnodes[element.childnodes.lelgth-1]

If you want to know if an element has child nodes, you can use the HasChildNodes method, and if a node does not have any child nodes, its LastChild property will return null.

The LastChild property is a read-only property.

    • Nextsibing

The Nextsibing property returns the next child node of a given node:

Reference = Node.nextsibing

This property returns a pointer to the Node object reference, which has common properties such as NodeType, NodeName, NodeValue, and so on.

If there is no node behind the given node that belongs to the parent node, its NextSibling property returns NULL.

The NextSibling property is a read-only property.

    • ParentNode

The ParentNode property returns the parent node of a given node:

Reference = Node.parentnode

This property returns a reference pointer to a node object that has common node properties such as NodeType, NodeName, NodeValue, and so on.

The node returned by the ParentNode property is always an element node, because only the element node can contain child nodes, the only exception being the document node, which has no parent node, in other words, the ParentNode property of the document node will return NULL.

The ParentNode property is a read-only property.

    • PreviousSibling
Reference  = node.previoussibling

This property returns a reference pointer to the previous node object, which has common node properties such as NodeType, NodeName, NodeValue, and so on.

If no previous element node returns NULL.

The PreviousSibling property is a read-only property.

Traversing a node tree

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.