JavaScript Getting Started learning Eighth JS DOM Node Properties description 1th/2 Page _ Basics

Source: Internet
Author: User
Tags uppercase letter

Today we talk about DOM properties.
We've actually touched the DOM attribute before.
Like what:
Nodename,nodetype. Today we explain in detail.


1,nodename Property: The name of the node.
If the node is an element node, then the name of the element is returned. At this point, the equivalent TagName property.
Like what:
<p>aaaa</p>: Then return p;
If it is an attribute node, NodeName returns the name of the property.
If it is a text node, NodeName returns a #text string.

In addition, I would say that the NodeName property is a read-only property and cannot be set. Write
It returns the value of the uppercase letter.

2,,nodetype property: Returns an Integer that represents the type of the node.
The 3 types we use are:
NodeType = 1: Element node
NodeType = 2: Attribute node
NodeType = 3: Text node
If you want to remember, we can remember this:
For example: <p title= "Cssrain" >test</p> read after: You will find the element node (1), then the attribute node (2), and finally the text node (3), so you can easily remember what type NodeType represents. (A little tip I've summed up, ^_^.) )

The NodeType property is often used in conjunction with if to ensure that the wrong operation is not performed on the wrong node type.
Like what:
function Cs_demo (mynode) {
if (Mynode.nodetype = = 1) {
Mynode.setattribute ("title", "Demo");
}
}
Code Explanation: First check the Mynode NodeType property to make sure that the node it represents is indeed an element node.
Like the NodeName property, he is also a read-only property and cannot be set. (write).

3,nodevalue property: Returns a String that is the value of this node.
Returns null if the node is an element node; (note below)
If it is an attribute node, NodeValue returns the value of this property.
If it is a text node, NodeValue will return the contents of this text node.
Like what:
<div id= "C" >aaaaaaaaaaaaaaaa</div>
<script language= "JavaScript" >
var c= document.getElementById ("C");
alert (c.nodevalue);//return NULL
</SCRIPT>
NodeValue is an attribute that can be read and written. However, it cannot set the value of an element node.
Take another look at the following example:
<div id= "C" >aaaaaaaaaaaaaaaa</div>
<script language= "JavaScript" >
var c= document.getElementById ("C");
C.nodevalue = "DDDDDDDDDDDD"; Cannot set
The alert (c.firstchild.nodevalue)//Element node includes an attribute node and a text node.
C.firstchild.nodevalue = "Test"//can set
</SCRIPT>
Of course we can add a code to make sure that it works correctly:
<div id= "C" >aaaaaaaaaaaaaaaa</div>
<script language= "JavaScript" >
var c= document.getElementById ("C");
C.nodevalue = "DDDDDDDDDDDD"; Cannot set
Alert (C.firstchild.nodevalue)
if (c.firstchild.nodetype==3) {//Judge is not a text node
C.firstchild.nodevalue = "Test"//can set
}
</SCRIPT>
As you can see, if you want to set the element node, you cannot set it directly, but you must first use FirstChild or LastChild, and then set the NodeValue.
NodeValue is typically used only to set the value of a text node. If you want to refresh the value of an attribute node, you typically use setattribute ().
Current 1/2 page 12 Next read the full text
Related Article

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.