Home > Web Front-end > JS Tutorial > JavaScript deletes specified child element code example_javascript skills

JavaScript deletes specified child element code example_javascript skills

WBOY
Release: 2016-05-16 16:20:19
Original
1445 people have browsed it

Native javascript code example to delete specified sub-element:

This chapter introduces how to use native javascript to delete specified sub-elements.
Everyone knows that it is more convenient to use jquery to implement this function, but it is not troublesome to use native javascript. Let’s introduce it below.
For information about how jquery implements this function, please refer to the chapter jquery to delete specified child elements code examples.

Code example:

Copy code The code is as follows:




Script Home

<script><br> window.onload=function(){<br> var obt=document.getElementById("bt");<br> var obox=document.getElementById("box");<br> var lis=obox.getElementsByTagName("li");<br> obt.onclick=function(){<br> obox.removeChild(lis[1]);<br> }<br> }<br> </script>



  • Script House welcomes you, only by working hard can we have a better tomorrow.

  • No one is a master from the beginning, they have to work hard.

  • Every day is new, so cherish your time.





The above code achieves our requirements. Here is an introduction to its implementation process.

Code comments:

1.window.onload=function(){}, when the document content is completely loaded, execute the code in the function.
2.var obt=document.getElementById("bt"), 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"), get the collection of li elements under the box element.
5.obt.onclick=function(){}, register the click event processing function for the button.
6.obox.removeChild(lis[1]), deletes the specified child element of the parent element.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template