jQuery 元素移除empty() remove()與detach()的區別?

來源:互聯網
上載者:User

標籤:

@1.empty() 刪除匹配元素集合中所有的後代位元組點元素;

         <p>hello<span>world</span></p>

         $("p").empty();   <p></p>

 

@2.remove([expr]) 接收參數表示刪除指定的元素

$("p").remove()  刪除所有段落

$("p").remove(".hel") 刪除帶有.hel類名的段落

這個方法不會把匹配的元素從jQuery對象中刪除,因而可以在將來再使用這些匹配的元素。但除了這個元素本身得以保留之外,其他的比如綁定的事件,附加的資料等都會被移除

 

@3.detach([expre]) 從DOM中刪除所有匹配的元素

這個方法不會把匹配的元素從jQuery對象中刪除,因而可以在將來再使用這些匹配的元素。與remove()不同的是,所有綁定的事件、附加的資料等都會保留下來

<body>    

         <div id="google">Google</div>

<div id="apple">Apple</div>

</body>

<script type="text/javascript">

          $(function () {

        $("#apple").hover(function () {

            $(this).text("Google+");

        });

        apple = $("#apple").remove();   <!-- //使用 remove() hover()事件也會被刪除 -->

       /*apple = $("#apple").detach();*/<!--  //使用detach() hover()事件會儲存下來 -->

        $("body").append(apple);         

    });

</script>

jQuery 元素移除empty() remove()與detach()的區別?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.