Example of deleting a node using jQuery's DOM operation

Source: Internet
Author: User

If an element in the document is redundant, delete it. JQuery provides two methods to delete nodes: The remove () method and the empty () method.

1. remove ():
Copy codeThe Code is as follows:
<% @ Page contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Insert title here </title>
<Script src = "jQuery/jquery-1.10.2.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Ul li: eq (1)"). remove ();
});

</Script>
</Head>
<Body>
<P title = "what kind of fruit do you like best? "> What is your favorite fruit? </P>
<Ul>
<Li title = "apple"> apple </li>
<Li title = "orange"> orange </li>
<Li title = "banana"> bananas </li>
</Ul>
</Body>
</Html>

 
When a node is deleted using the remove method, all child nodes contained in the node will be deleted at the same time. The return value of this method is a reference pointing to the deleted node.

In addition, the remove () method can also selectively Delete elements by passing parameters.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Ul li"). remove ("li [title! = Apple] ");
});
</Script>


2. empty ():

Strictly speaking, the empty method does not delete nodes, but clears nodes. It can clear all child nodes in the element.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Ul li: eq (1)"). empty ();
});
</Script>

 
View the page source code:

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.