JS Judgment Node type

Source: Internet
Author: User

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

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.