今天就跟大家聊聊有關(guān)清空元素html("") innerHTML="" 與 empty()的區(qū)別和應(yīng)用有哪些,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

在寧陽等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站 網(wǎng)站設(shè)計制作定制網(wǎng)站制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),營銷型網(wǎng)站,外貿(mào)網(wǎng)站制作,寧陽網(wǎng)站建設(shè)費用合理。
一、清空元素的區(qū)別
1、錯誤做法一:
$("#test").html("");//該做法會導致內(nèi)存泄露
2、錯誤做法二:
$("#test")[0].innerHTML=""; ;//該做法會導致內(nèi)存泄露
3、正確做法:
//$("#test").empty();
二、原理:
在 jQuery 中用 innerHTML 的方法來清空元素,是必然會導致內(nèi)存泄露的,由于 jquery 對于同一元素多事件處理沒有直接采用瀏覽器事件模型,而是自己緩存事件,遍歷觸發(fā),以及便于 trigger 程序觸發(fā) :
// Init the element's event structure
var events = jQuery.data(elem, "events") || jQuery.data(elem, "events", {}),
handle = jQuery.data(elem, "handle") || jQuery.data(elem, "handle", function(){
// Handle the second event of a trigger and when
// an event is called after a page has unloaded
return typeof jQuery !== "undefined" && !jQuery.event.triggered ?
jQuery.event.handle.apply(arguments.callee.elem, arguments) :
undefined;
});采用 data 方法,將一些數(shù)據(jù)關(guān)聯(lián)到了元素上面,上述事件即是采用該機制緩存事件監(jiān)聽器。
那么就可以知道,直接 innerHTML=“” 而不通知 jquery 清空與將要刪除元素關(guān)聯(lián)的數(shù)據(jù),那么這部分數(shù)據(jù)就再也釋放不了了,即為內(nèi)存泄露。
remove: function( selector ) {
if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
// Prevent memory leaks
jQuery( "*", this ).add([this]).each(function(){
jQuery.event.remove(this);
jQuery.removeData(this);
});
if (this.parentNode)
this.parentNode.removeChild( this );
}
},
empty: function() {
// Remove element nodes and prevent memory leaks
jQuery(this).children().remove();
// Remove any remaining nodes
while ( this.firstChild )
this.removeChild( this.firstChild );
}看完上述內(nèi)容,你們對清空元素html("") innerHTML="" 與 empty()的區(qū)別和應(yīng)用有哪些有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享標題:清空元素html("")innerHTML=""與empty()的區(qū)別和應(yīng)用有哪些
文章位置:http://www.chinadenli.net/article2/gpcooc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、靜態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化、網(wǎng)站維護、移動網(wǎng)站建設(shè)、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)