This article mainly introduces the code example for deleting the specified child element in JavaScript. This article provides code examples and implementation code explanations. if you need it, refer to the example for deleting the specified child element in native javascript:
This topic describes how to use native javascript to delete specified child elements.
We all know that it is more convenient to use jquery to implement this function, but it is not difficult to use native javascript. The following is an introduction.
For more information about how jquery can implement this function, see the section "delete subelement code instance" in jquery.
Code example:
The code is as follows:
Foot home
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
- Thank you for choosing the best team. only by working hard can we have a bright tomorrow.
- No one is a master at the beginning. you must work hard.
- Every day is new, so cherish the time.
The above code implements our requirements. The following describes the implementation process.
Code comment:
1. window. onload = function () {}, when the content of the document is fully loaded, run the code in the function.
2. var obt = document. getElementById ("bt") to get the button element object.
3. var obox = document. getElementById ("box"), get the element object whose id attribute value is box.
4. var lis = obox. getElementsByTagName ("li") to obtain the li element set under the box element.
5. obt. onclick = function () {}, and register the click event processing function for the button.
6. obox. removeChild (lis [1]) to delete the specified child element of the parent element.