詳解jQuery中的empty、remove和detach
通過一張對(duì)比表來解釋幾個(gè)方法之間的不同
三者都有把元素移除的作用,但細(xì)微的差別,造就了它們的使命不同。
最權(quán)威的解釋當(dāng)然是jQuery_API咯,下面是API中關(guān)于他三兒的部分截取。
一、empty:
This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element is considered a child node of that element.To avoid memory leaks, jQuery removes other constructs such as data and event handlers from the child elements before removing the elements themselves. If you want to remove elements without destroying their data or event handlers (so they can be re-added later), use .detach() instead.
注意:加粗的部分,通過empty移除后代元素,會(huì)移除其事件的。
為什么呢?
防止內(nèi)存泄露?。。?br />
二、remove:
Similar to .empty(), the .remove() method takes elements out of the DOM. Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. To remove the elements without removing data and events, use .detach() instead.
remove和empty方法一樣,都會(huì)移除元素的事件句柄,從而避免內(nèi)存泄露。
區(qū)別:remove包含了移除事件本身,而empty是后代元素。
三、detach:
從empty和remove的介紹中(英文斜體部分),可以或多或少得知,detach是不會(huì)移除事件句柄的。
那么我們?cè)賮砜纯丛敿?xì)的API講解:
The .detach() method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.
咦,什么意思?
看了detach的注解,不知道大家有沒有眼前一亮,detach不能用來刪除廢棄的元素。
為什么呢?
因?yàn)樗A袅耸录?qū)動(dòng)嘛,這樣不就會(huì)造成內(nèi)存泄露么。
所以要?jiǎng)h除以后不再利用的元素時(shí),使用empty或者remove。
那要detach有何用?
用處大了。
當(dāng)我們要對(duì)一個(gè)元素進(jìn)行大規(guī)模的增刪改的時(shí)候,我們可以用detach將這個(gè)元素提取出來,然后在這個(gè)元素上進(jìn)行操作,而不是在整個(gè)dom文檔中進(jìn)行操作。
好處就是:減少對(duì)整個(gè)dom文檔的修改,從而減少頁(yè)面重繪;而且對(duì)整個(gè)dom文檔進(jìn)行操作,在ie下還可能會(huì)造成內(nèi)存泄露哦。所以穩(wěn)妥起見,還是利用detach這一神器吧。
下面是一個(gè)demo,首先對(duì)#container元素綁定click事件(事件委托),然后利用detach將其脫離文檔,然后再創(chuàng)建兩個(gè)child元素,追加到#container元素中,最后將#container重新添加到body后。
<!DOCTYPE html> <head> <title>jQuery</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <style> div.monkey, #container { width:120px; height:120px; line-height:60px; } div.monkey { border:1px solid black; } </style> </head> <body> <div class="monkey"> </div> <div id="container"> </div> <script src="jquery-1.12.0.js"></script> <script> $(function(){ //事件代理 $('#container').on('click',function( event ){ console.log( $(event.target).text() ); }); //利用detach將container從dom文檔中剝離開 var container = $('#container').detach(); var child1 = '<div>I am Monkey</div>'; var child2 = '<div>Monkey is me</div>'; //將child1、child2插入container中 $(container).append( child1 ) .append( child2 ); //將container重新插入body中 $('body').append( container ); }); </script> </body> </html>
以上所述是小編給大家介紹的jQuery中的empty、remove和detach的區(qū)別,希望對(duì)大家有所幫助!
相關(guān)文章
jquery綁定事件 bind和on的用法與區(qū)別分析
這篇文章主要介紹了jquery綁定事件 bind和on的用法與區(qū)別,結(jié)合實(shí)例形式分析了jquery綁定事件 bind和on的基本功能、用法、區(qū)別與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05在一個(gè)頁(yè)面實(shí)現(xiàn)兩個(gè)zTree聯(lián)動(dòng)的方法
最近發(fā)現(xiàn)項(xiàng)目中很多地方都是樹形菜單,而這些樹形菜單都是使用樹形插件zTree來制作的,下面這篇文章主要給大家介紹了關(guān)于在一個(gè)頁(yè)面實(shí)現(xiàn)兩個(gè)zTree聯(lián)動(dòng)的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12JQuery上傳插件Uploadify使用詳解及錯(cuò)誤處理
關(guān)于JQuery上傳插件Uploadify使用詳解網(wǎng)上一大把,基本上內(nèi)容都一樣。我根據(jù)網(wǎng)上的步驟配置完成后,會(huì)報(bào)一些錯(cuò)誤,而我根據(jù)這些錯(cuò)誤去網(wǎng)上找解決方案,卻沒有相關(guān)資料,所以為了不讓更多的朋友走彎路,我把我遇到的一些問題進(jìn)行匯總,也方便我自己以后查閱。2010-04-04jQuery構(gòu)造函數(shù)init參數(shù)分析續(xù)
其實(shí)樓主的F和jQuery.fn.init是相等的; 實(shí)現(xiàn)功能是和jq一樣的, 只是jq的把構(gòu)造函數(shù)放進(jìn)原型;如果非要說原因,個(gè)人理解jq這樣寫整體結(jié)構(gòu)清晰,先是入口構(gòu)造函數(shù),緊跟著是原型部分(原型里面init是初始化),但是不好理解;乍一看確實(shí)挺繞, 我也是看了好久才明白怎么回事2015-05-05JQuery EasyUI 結(jié)合ztrIee的后臺(tái)頁(yè)面開發(fā)實(shí)例
下面小編就為大家?guī)硪黄狫Query EasyUI 結(jié)合ztrIee的后臺(tái)頁(yè)面開發(fā)實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09jQuery的實(shí)現(xiàn)原理的模擬代碼 -1 核心部分
最近又看了一下 jQuery 1.4.2, 為了便于理解,將 jQuery 的核心使用比較簡(jiǎn)單的代碼模擬一下。方便學(xué)習(xí)。2010-08-08Jquery解析json字符串及json數(shù)組的方法
這篇文章主要介紹了Jquery解析json字符串及json數(shù)組的方法,實(shí)例分析了jQuery操作json格式字符串與數(shù)組的相關(guān)技巧,需要的朋友可以參考下2015-05-05