Dom nodename nodetype nodevalue

Source: Internet
Author: User
Tags uppercase letter

1. nodename attribute: name of the node.
If the node is an element node, the name of the element is returned. In this case, it is equivalent to the tagname attribute. For example, <p> AAAA </P>: returns p.
If it is an attribute node, nodename returns the name of this attribute.
If it is a text node, nodename returns a string of # text.

In addition, I want to say that the nodename attribute is a read-only attribute and cannot be set. (write)It returns the value of an uppercase letter.

2, nodetype property: returns an integer representing the type of the node.
Common three types:
Nodetype = 1: Element Node
Nodetype = 2: attribute node
Nodetype = 3: text node
If you want to remember it, we can remember it like this:
For example: <P Title = "cssrain"> test </P> read from the past to the next: you will findElement Node (1), attribute node (2), and text node (3 ),In this way, you can easily remember what types nodetype represents. (A little trick I have summarized, ^_^ .)

The nodetype attribute is often used with if to ensure that incorrect operations are not performed on the wrong node type.
For example:
Function cs_demo (mynode ){
If (mynode. nodetype = 1 ){
Mynode. setattribute ("title", "Demo ");
}
}
CodeExplanation: first check the nodetype attribute of mynode to ensure that the node it represents is indeed an element node.
Like the nodename attribute, it is also a read-only attribute and cannot be set. (write ).

3. nodevalue attribute: return a string of the node value.
If the node is an element node, null is returned. (note)
If it is an attribute node, nodevalue returns the value of this attribute.
If it is a text node, nodevalue returns the content of this text node.
For example:
<Div id = "C"> aaaaaaaaaaaaaaaa </div>
<Script language = "JavaScript">
VaR c = Document. getelementbyid ("C ");
Alert (C. nodevalue); // return null
</SCRIPT>
Nodevalue is a readable and writable attribute. However, it cannot set the value of the element node.
Let's take a look at the following example:
<Div id = "C"> aaaaaaaaaaaaaaaa </div>
<Script language = "JavaScript">
VaR c = Document. getelementbyid ("C ");
C. nodevalue = "dddddddddddd"; // cannot be set
// Alert (C. firstchild. nodevalue) // element nodes include attribute nodes and text nodes.
C. firstchild. nodevalue = "test" // can be set
</SCRIPT>
Of course, to ensure proper running: you can add a piece of code:
<Div id = "C"> aaaaaaaaaaaaaaaa </div>
<Script language = "JavaScript">
VaR c = Document. getelementbyid ("C ");
C. nodevalue = "dddddddddddd"; // cannot be set
// Alert (C. firstchild. nodevalue)
If (C. firstchild. nodetype = 3) {// determines whether the node is a text node.
C. firstchild. nodevalue = "test" // can be set
}
</SCRIPT>
// You can see that if you want to set element nodes, you cannot directly set them. Instead, you must first use firstchild or lastchild and then set nodevalue.
Nodevalue is generally only used to set the value of a text node. If you want to refresh the value of an attribute node, setattribute () is generally used ().

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.