Native JS Learning Note 4--bom operation
What is DOM
Dom:do
1. JS Components
2. A set of standards, which currently have both DOM1 and DOM2
We can use DOM actions to manipulate elements in the Page.
DOM node
Child nodes
? Childnode: returns an array of values, with all the child nodes in the parent node
? NodeType: The return value is an array, 1 represents an element node, and 3 represents a text node
In some browsers, such as firefox, the use of Childnode to get all the elements of the node when the space will be taken together or removed, you need to use NodeType to Judge.
Example:
<! DOCTYPE html><html lang= "en" ><head>
<meta charset= "UTF-8" >
<title>document</title>
<script type= "text/javascript" >
Use Childnode to get all the Li tags and change the background color to red
Window.onload = function () {
var oul = document.getElementById (' ul ');
for (var i = 0; i < oUl.childNodes.length; i++) {
Determine if the value of NodeType is 1
if (oul.childnodes[i].nodetype = = 1) {
Oul.childnodes[i].style.backgroundcolor = ' Red ';
};
};
}
</script></head><body>
<ul id = ' UL ' >
<Li></li>
<Li></li>
<Li></li>
</ul></body></HTML>
? Childern: take child nodes, compared to childnode, Chlidren will not parse non-tagged elements, do not worry about compatibility issues
Parent node
? ParentNode
Child nodes
Brother Node
adding elements
CreateElement (element name): Create an element
Parent Element. appendchild (child Element) adds child elements to the parent element (added on the last Side)
Example: Click the button once to add an LI tag
<! DOCTYPE html><html lang= "en" ><head>
<meta charset= "UTF-8" >
<title>document</title>
<script type= "text/javascript" >
Window.onload = function() {
var obtn = document.getElementById (' btn ');
var oul = document.getElementById (' ul ');
var otext = document.getElementById (' text ');
Obtn.onclick = function () {
Create a new label
var oLi = document.createelement (' li ');
Write the text in the input box in the new Li tag
oli.innerhtml = otext.value;
Add a new label to the parent tag
Oul.appendchild (oLi);
}
}
</script></head><body>
<input type= "text" id = "text" >
<input type= "button" value= "click add" id = "btn" >
<ul id = "ul" >
<Li>hello</li>
</ul></body></HTML>
Parent Element. InsertBefore (child element, who was inserted before), who inserted the child element before
<! DOCTYPE html><html lang= "en" ><head>
<meta charset= "UTF-8" >
<title>document</title>
<script type= "text/javascript" >
Window.onload = function() {
var obtn = document.getElementById (' btn ');
var oul = document.getElementById (' ul ');
var otext = document.getElementById (' text ');
Obtn.onclick = function () {
Create a new label
var oLi = document.createelement (' li ');
Write the text in the input box in the new Li tag
oli.innerhtml = otext.value;
Add a new label to the parent tag
Oul.appendchild (oLi);
var aLi = document.getElementsByTagName (' li ');
Insert a new element before the No. 0 element
Oul.insertbefore (oLi, ali[0]);
}
}
</script></head><body>
<input type= "text" id = "text" >
<input type= "button" value= "click add" id = "btn" >
<ul id = "ul" >
<Li>hello</li>
</ul></body></HTML>
Delete Element
Parent Element. removechild (element to delete): Delete a child element in the parent element
Example: Click Delete to delete an element
<! DOCTYPE html><html lang= "en" ><head>
<meta charset= "UTF-8" >
<title>document</title>
<script type= "text/javascript" >
Window.onload = function() {
var obtn = document.getElementsByTagName (' a ');
var oul = document.getElementById (' ul ');
for (var i = 0; i < obtn.length; i++) {
Obtn[i].onclick = function() {
Oul.removechild (this. parentnode);
}
};
}
</Script>
</Head><body>
<input type= "text" id = "text" >
<input type= "button" value= "click add" id = "btn" >
<ul id = "ul" >
<Li>hello<a href= "javascript:;" > Delete </a></li>
<Li>123<a href= "javascript:;" > Delete </a></li>
<Li>234<a href= "javascript:;" > Delete </a></li>
<Li>hel234lo<a href= "javascript:;" > Delete </a></li>
</ul></body></HTML>
Native JavaScript Fourth