Empty
Remove child elements of all paragraphs (including text nodes)
HTML Code:
<p>Hello, <span>Person</span> <a href="#">and person</a></p>
JQuery Code:
$("p").empty();
Results:
<p></p>
Remove
Removes all matching elements from the DOM.
This method does not remove the matching elements from the jquery object, and the matching elements can be used in the future. But besides the element itself is preserved, other such as bound events, additional data, etc. will be removed .
that is, empty retains itself, and remove removes itself .
Detach
Removes all matching elements from the DOM.
This method does not remove the matching elements from the jquery object, so that the matching elements can be used in the future. unlike remove (), all bound events, attached data, and so on are preserved .
The following emphasis is given to an example of the nature of the event and data detach not remove the element bindings.
<!DOCTYPE HTML><HTML> <Head> <MetaCharSet= "Utf-8" /> <title></title> <Scripttype= "Text/javascript"src= "Jquery-1.11.0.js" ></Script> <Scripttype= "Text/javascript"> $(function() { var$div 2=$("#div2"); $div 2.data ("value", 1); $("#detach"). On ("Click", function() {$div 2.detach (); }); $("#back"). On ("Click", function() { $("#div1"). Append ($div 2); Console.log ($ ("#div2"). Data ("value")); }); }); </Script> </Head> <Body> <DivID= "Div1"> <DivID= "Div2">Div2</Div> <DivID= "Div3">Div3</Div> </Div> <inputvalue= "Detach"ID= "Detach"type= "button" /> <inputvalue= "Back"ID= "Back"type= "button" /> </Body></HTML>
If you change detach to remove, the console appears as undefined after you click Back.
Note The return value of detach, returning the element that was detach .
var p;
$( "button" ).click(function() {
if ( p ) {
p.appendTo( "body" );
p = null;
} else {
p = $( "p" ).detach();
}
});
Reference: http://blog.csdn.net/qinshenxue/article/details/23832221