NodeType, NodeName and NodeValue

Source: Internet
Author: User

First of all, there are three major nodes in the DOM, namely, element node, text node, attribute node

ELEMENT node: Forms the basis of the DOM. ,

Text node: contained in the element node.

Attribute nodes: Elements can contain attributes that are used to make more specific descriptions of elements, such as Id,name.

Take the following demo as an example:

<script>function() {    var element = document.getElementById ("span" );   var text = element.firstchild;     var property = document.getElementById ("span"). GetAttributeNode ("id"); }</script><body> <div>  <span id= "span" > Text node </span> </div>< /body>

( a) The NodeName property contains the name of a node.

The nodeName of the element node is the label name (the returned name is uppercase)

The nodeName of the attribute node is the property name (the returned name is uppercase)

The nodeName of a text node is always #text

The nodeName of the document node is always #document

Note: The label name of the XML element contained in NodeName is always capitalized

Alert ("This is the return value of the element node:" + Element.nodename); // return the label name span, note that it is uppercase alert ("This is the return value of the text node:" + Text.nodename); // The returned #textalert ("This is the return value of the attribute node:" + Property.nodename); // returned is the property name, here is the ID

( II) nodevalue

For text nodes, the NodeValue property contains text.

For attribute nodes, the NodeValue property contains the property value.

The NodeValue property is not available for document nodes and ELEMENT nodes.

Alert ("This is the return value of the element node:" + Element.nodevalue); // itself is meaningless, here is the experiment, and the return is nullalert ("This is the return value of the text node:" + Text.nodevalue); // returns the text value of the  text node alert ("This is the return value of the attribute node:" + Property.nodevalue); // returns the property value, which is the property value of the ID  myspan

( c) NodeType

The NodeType property returns the type of the node.

3 of the types we use:
NodeType = = 1: element node
NodeType = = 2: Attribute node
NodeType = = 3: Text node

Note Comments 8

Document 9

  Alert ("This is the return value of the element node:" + Element.nodetype); // element Node returns 1  Alert ("This is the return value of the text node:" + Text.nodetype); // text node returns 3  Alert ("This is the return value of the attribute node:" + Property.nodetype); // Property node returns 2

___________________________

Original: Http://www.tuicool.com/articles/zANnye

NodeType, NodeName and NodeValue

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.