In the last two articles, we discussed how to use dom to create nodes, copy nodes, insert nodes, delete nodes, replace nodes, find nodes, and obtain attributes... In the last two articles, we discussed how to use dom to create nodes, copy nodes, insert nodes, delete nodes, replace nodes, find nodes, and obtain attributes...
Today we will talk about DOM attributes.
We have already touched DOM attributes.
For example:
NodeName, nodeType ..... Today we will explain in detail.
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:
Aaaa
: 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:
Test
Read from the past: you will find that first the Element Node (1), then the attribute node (2), and finally the 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 ");
}
}
Code explanation: 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:
Aaaaaaaaaaaaaaaa