After understanding the framework and nodes of DOM (Text Object Model), the most important thing is to use these nodes to process html web pages
For a DOM node node, there are a series of properties and methods that can be used. Commonly used ones include the following table.
Perfect: http://www.w3school.com.cn/xmldom/dom_element.asp
1. Access node
BOM provides some boundary methods to access nodes, the commonly used ones are getElementsByTagName(), and getElementById()
document.getElementById()
//id gets className
2. Detect node type
The type of node can be detected through the nodeType. This parameter returns 12 integer values.
Expression format such as document.nodeType
What is really useful are the three types of model nodes mentioned in the DOM (1) model
Element nodes, text nodes and attribute nodes
1. The return value of element node is 1
2. The attribute node return value is 2
3. The text node text node returns a value of 3
This means that certain nodes can be processed individually, which is very practical when searching for nodes. Will talk about it later.
3. Use the relationship between father, son and brother to find nodes
When accessing the node in the first section, use the childNodes attribute of the node to access the text node contained in the element node.
This section uses the parent-child-brother relationship of nodes to find nodes
*Use the hasChildNodes and childNodes attributes to get all the nodes contained in this node
Using the parent node, the parent node of the specified node was successfully obtained
5. Use parentNode attribute
6.dom’s brotherly relationship
In order to have good compatibility when using the code, nodeType must be used for judgment
Compatibility processing is as follows:
Set node attributes using the setAttribute() method
There is a P here, test createElement()
The code is as follows: