Classification of node types
Node type |
Description |
Value |
ELEMENT node |
Each HTML tag is an element node, such as <div>, <p>, <ul>, and so on |
1 |
Attribute node |
Attributes of ELEMENT nodes (HTML tags), such as ID, class, name, and so on. |
2 |
Text node |
The text content in the element node or attribute node. |
3 |
Comment Node |
Represents a document comment in the form <!--comment text---. |
8 |
Document node |
Represents the entire document (the root node of the DOM tree, which is document) |
9 |
NodeType, NodeName, Nodevale determine node type, node name, and node value
<!--using JavaScript to determine the node type--><div id= "Onediv" > a text </div><!--comment text--><script type= "text/ JavaScript "> var div = document.getElementById (" Onediv "); Console.log (Div.nodetype); Output 1, Element node var divtext = div.firstchild; Console.log (divtext.nodetype)//output 3, text node var divattr = Div.getattributenode ("id"); Console.log (divattr.nodetype)//output 2, attribute node var comment = div.nextsibling; Console.log (comment.nodetype)//output 8, note node </script>
<!--using JavaScript to determine the node name--><div id= "Onediv" > a text </div><!--comment text--><script type= "text/ JavaScript "> var div = document.getElementById (" Onediv "); Console.log (Div.nodename); Output div, element node is tag capital var divtext = div.firstchild; Console.log (divtext.nodename)//output #text, text nodes using nodeName always #text var divattr = Div.getattributenode ("id"); Console.log (divattr.nodename)//output ID, attribute node is the property name var comment = div.nextsibling; Console.log (comment.nodename)//output #comment, note nodes are always #comment</script> when using NodeName
<!--use JavaScript to determine node values--><div id= "Onediv" > a text </div><!--comment text--><script type= "text/ JavaScript "> var div = document.getElementById (" Onediv "); Console.log (Div.nodevalue); Output null, element node for NodeValue does not support var divtext = div.firstchild; Console.log (divtext.nodevalue)//output a text, text node output text value var divattr = Div.getattributenode ("id"); Console.log (divattr.nodevalue)//output Onediv, attribute node output attribute value var comment = div.nextsibling; Console.log (comment.nodevalue)//Output comment text, comment node output comment content </script>
JS Judgment Node type