PHP Remove,empty and Detach differences

Source: Internet
Author: User

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

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.