The DOM node deletes the empty and remove

Source: Internet
Author: User

The DOM node deletes the empty and remove

Just learned new knowledge, although it is a small knowledge point, but still can't help but want to share with you.

. Empty () is the deletion of descendants of that node, with the result that the node is emptied (there are no elements inside that node).

. Remove () is the deletion of the node, which results in the deletion of the node (the node and its descendant elements will not exist).

Put the code below to illustrate.

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<script src= "Http://libs.baidu.com/jquery/1.9.1/jquery.js" ></script>
<style>
body{
Background: #ffe5aa;
}
#test1 {
width:100px;
height:100px;
Background: #3db7ff;
}
#test2 {
width:100px;
height:100px;
Background: #ff179f;
}
</style>
<body>
<div class= "whole" >
<button class= "bt1" > Delete nodes via empty </button>
<button class= "BT2" > Remove nodes via Remove </button>
</div>
<div id= "Test1" >
<p> Elemental 1</p>
<p> Elemental 2</p>
</div>
<div id= "Test2" >
<p> Elemental 3</p>
<p> Elemental 4</p>
</div>
<script type= "Text/javascript" >
$ (". Bt1"). On (' click ', function () {
$ ("#test1"). empty ();
})
$ (". Bt2"). On (' click ', function () {
$ ("#test2"). Remove ();
})
</script>
</body>
When you click Run, the following screen appears.

Click Button1, the. Empty () directive is executed, and the Test1 child node element is expected to be deleted. The results are as follows.

Click Button2 again to execute the. Remove () directive. It is expected that the test2 and its descendants child node elements will be deleted. The results are as follows.

The above is the DOM node deletion of the difference between empty and remove, the first time to write a blog, not good please understand.

The DOM node deletes the empty and remove

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.