In the DOM Document Object Model), the HTML document hierarchy is represented as a tree structure. The tree structure of the HTML document mainly consists of nodes that represent elements or tags and nodes that identify text strings. In JavaScript DOM, Node is often translated into a Node. Next we will use an instance to learn about Node attributes and methods.
BKJIA recommended reading: An in-depth explanation of BOM and DOM in JavaScript
Node attributes:
- NodeType: displays the node type.
- NodeName: displays the node name.
- NodeValue: displays the value of a node.
- Attributes: Get an attribute node
- FirstChild: indicates the first node of a node.
- LastChild: The last subnode of a node.
- ChildNodes: indicates all the subnodes of the node.
- ParentNode: indicates the parent node of the node.
- NextSibling: next to the current node
- Previussibling: next to the previous node of the current node
Node has a variety of nodes. We will spend some time to get to know them and learn about the nodeType, nodeName, and nodeValue attributes at the same time:
Name: Element Node
◆ NodeType: ELEMENT_NODE
◆ NodeType value: 1
◆ NodeName: Element Tag Name
◆ NodeValue: null
- <Body>
- <Div id = "t"> <span> </div>
- </Body>
- <Script>
- Var d = document. getElementById ("t ");
- Document. write (d. nodeType );
- Document. write (d. nodeName );
- Document. write (d. nodeValue );
- // Display 1 DIV null
- </Script>
Name: attribute node
◆ NodeType: ATTRIBUTE_NODE
◆ NodeType value: 2
◆ NodeName: attribute name
◆ NodeValue: Attribute Value
- <Body>
- <Div id = "t" name = "aaa"> <span> </div>
- </Body>
- <Script>
- Var d = document. getElementById ("t"). getAttributeNode ("name ");
- Document. write (d. nodeType );
- Document. write (d. nodeName );
- Document. write (d. nodeValue );
- // Display 2 name aaa
- </Script>
Name: text node
◆ NodeType: TEXT_NODE
◆ NodeType value: 3
◆ NodeName: # text
◆ NodeValue: Text Content
- <Body>
- <Div id = "t"> bbb </div>
- </Body>
- <Script>
- Var d = document. getElementById ("t"). firstChild;
- Document. write (d. nodeType );
- Document. write (d. nodeName );
- Document. write (d. nodeValue );
- // Display 3 # text bbb
- </Script>
Name: Format of text transmitted in CDATA text node XML)
◆ NodeType: CDATA_SECTION_NODE
◆ NodeType value: 4
◆ NodeName: # cdata-section
◆ NodeValue: CDATA text content
Attributes attribute to directly obtain an attribute node. Note that [] should be used here to maintain compatibility between IE and FF.
- <Body name = "ddd">
- <Div id = "t" name = "aaa"> <span> aaa </span> <span> bbb </span> <span> ccc </span> </div>
- </Body>
- <Script>
- Var d = document. getElementById ("t"). attributes ["name"];
- Document. write (d. name );
- Document. write (d. value );
- // Display name aaa
- </Script>
The firstChild and lastChild attributes indicate the first and last subnodes of a node:
- <Body>
- <Div id = "t"> <span> aaa </span> <span> bbb </span> <span> ccc </span> </div>
- </Body>
- <Script>
- Var d = document. getElementById ("t ");
- Document. write (d. firstChild. innerHTML );
- Document. write (d. lastChild. innerHTML );
- // Display aaa ccc
- </Script>
The childNodes and parentNode attributes indicate all the child nodes of the node and the parent nodes of the node. The childNodes here is an array:
- <Body name = "ddd">
- <Div id = "t"> <span> aaa </span> <span> bbb </span> <span> ccc </span> </div>
- </Body>
- <Script>
- Var d = document. getElementById ("t ");
- Document. write (d. childNodes [1]. innerHTML );
- Document. write (d. parentNode. getAttribute ("name "));
- // Display bbb ddd
- </Script>
The nextSibling and previussibling attributes indicate that, in the childNodes [] array of parentNode, they are next to the previous and next nodes of the current node:
- <Body name = "ddd">
- <Div id = "t"> <span> aaa </span> <span> bbb </span> <span> ccc </span> </div>
- </Body>
- <Script>
- Var d = document. getElementById ("t"). childNodes [1];
- Document. write (d. nextSibling. innerHTML );
- Document. write (d. previussibling. innerHTML );
- // Display ccc aaa
- </Script>