Three ways to delete nodes with jQuery
Nov 22, 2016 pm 02:13 PMHTML code used for testing:
<p title="Choose your favorite fruit?">What is your favorite fruit? </p>
<ul>
<li title="Apple">Apple</li>
<li title="Orange">Orange</li>
<li title= "pineapple">pineapple</li>
</ul>
1. remove() method
$("ul li").click(function(){
alert($(this).html ());
});
var $li = $("ul li:eq(1)").remove();
$li.appendTo("ul");
When a node uses remove( ) method, all descendant nodes contained in the node will be deleted at the same time. The return value of this method is a reference to the deleted node, so the elements can be used again later.
2. detach() method
var $li = $("ul li:eq(1)").detach();
$li.appendTo("ul");
detach() and remove( ), all matching elements are removed from the DOM. But it should be noted that this method will not delete the matching elements from the jQuery object, so these matching elements can be used again in the future. Unlike remove(), all bound events and additional data will be retained.
3. empty() method
var $li = $("ul li:eq(1)").empty();
$li.appendTo("ul");
Strictly speaking, empty( ) method does not delete the node, but clears the node. It clears all descendant nodes in the element.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of jQuery reference methods: Quick start guide

How to use PUT request method in jQuery?

How to remove the height attribute of an element with jQuery?

jQuery Tips: Quickly modify the text of all a tags on the page

Use jQuery to modify the text content of all a tags

In-depth analysis: jQuery's advantages and disadvantages

Understand the role and application scenarios of eq in jQuery

How to tell if a jQuery element has a specific attribute?
