JavaScript deletes the specified child element code instance _javascript tips

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.