麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

詳解jQuery中的empty、remove和detach

2019-11-20 10:16:42
字體:
來源:轉載
供稿:網(wǎng)友

 通過一張對比表來解釋幾個方法之間的不同

三者都有把元素移除的作用,但細微的差別,造就了它們的使命不同。

最權威的解釋當然是jQuery_API咯,下面是API中關于他三兒的部分截取。

一、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移除后代元素,會移除其事件的。

為什么呢?

防止內存泄露!!!

二、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方法一樣,都會移除元素的事件句柄,從而避免內存泄露。

區(qū)別:remove包含了移除事件本身,而empty是后代元素。

三、detach:

從empty和remove的介紹中(英文斜體部分),可以或多或少得知,detach是不會移除事件句柄的。

那么我們再來看看詳細的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不能用來刪除廢棄的元素。

為什么呢?

因為它保留了事件驅動嘛,這樣不就會造成內存泄露么。

所以要刪除以后不再利用的元素時,使用empty或者remove。

那要detach有何用?

用處大了。

當我們要對一個元素進行大規(guī)模的增刪改的時候,我們可以用detach將這個元素提取出來,然后在這個元素上進行操作,而不是在整個dom文檔中進行操作。

好處就是:減少對整個dom文檔的修改,從而減少頁面重繪;而且對整個dom文檔進行操作,在ie下還可能會造成內存泄露哦。所以穩(wěn)妥起見,還是利用detach這一神器吧。

下面是一個demo,首先對#container元素綁定click事件(事件委托),然后利用detach將其脫離文檔,然后再創(chuàng)建兩個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ū)別,希望對大家有所幫助!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美日韩手机在线观看 | 高清国产午夜精品久久久久久 | 欧美18videos性处按摩 | 国产高潮好爽受不了了夜色 | 久久精品成人 | 国产欧美精品综合一区 | pornoⅹxxxxhd麻豆 | 国产免费观看a大片的网站 欧美成人一级 | 午夜生活理论片 | gril hd| 嗯哈~不行好大h双性 | 毛片大全免费看 | 国产一区二区在线免费 | 午夜精品福利影院 | 午夜精品福利视频 | 色播视频在线播放 | 色婷婷久久一区二区 | 91九色电影 | 久久午夜神器 | 黄污网址 | 污片视频在线观看 | 91短视频网址 | 九九热视频在线 | 草久在线 | 黄色网址免费在线 | 成人在线视频播放 | 中文字幕观看 | 嗯~啊~弄嗯~啊h高潮视频 | 黄色免费在线视频网站 | 亚洲精品一区二区三区免 | 91九色免费视频 | 日韩1区 | 精品少妇v888av | 日韩精品a在线观看 | 中文字幕免费一区 | 久久国产精品99久久人人澡 | va视频在线 | 国产精品视频一区二区三区综合 | 亚洲第一精品在线 | 日本欧美一区二区三区在线观看 | 九九热免费视频在线观看 |