Native JavaScript deletes the specified child element code instance:
This section describes how to delete a specified child element using a native JavaScript implementation.
We all know it's more convenient to use jquery to do this, but it's no trouble to use native JavaScript, so here's a quick introduction.
About jquery How to implement this feature can refer to the jquery deletion section of the specified child element code instance.
Code instance:
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title> Cloud-dwelling community </title>
<style>
UL li{
width:400px;
height:30px;
line-height:30px;
List-style:none;
}
</style>
<script>
Window.onload=function () {
var Obt=document.getelementbyid ("BT");
var Obox=document.getelementbyid ("box");
var lis=obox.getelementsbytagname ("Li");
Obt.onclick=function () {
Obox.removechild (Lis[1]);
}
}
</script>
<body>
<ul id= "box" >
<li> Cloud Community welcomes you, only the struggle will have a better tomorrow. </li>
<li> no one is a master in the beginning, we must work hard to do. </li>
<li> every day is new, cherish the time well. </li>
</ul>
<input type= "button" id= "BT" value= "viewing effect"/>
</body>
The code above implements our requirements, the following is an introduction to its implementation process.
Code comments:
1.window.onload=function () {} to execute code in the function when the contents of the document are completely loaded.
2.var Obt=document.getelementbyid ("BT") to get the button element object.
3.var Obox=document.getelementbyid ("box"), gets the element object with the id attribute value of box.
4.var lis=obox.getelementsbytagname ("Li"), gets the collection of Li elements under the box element.
5.obt.onclick=function () {}, registering the Click event handler for the button.
6.obox.removechild (Lis[1]), deletes the specified child element of the parent element.