今天就跟大家聊聊有關(guān)清空元素html("") innerHTML="" 與 empty()的區(qū)別和應(yīng)用有哪些,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
在寧陽(yáng)等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作定制網(wǎng)站制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),營(yíng)銷型網(wǎng)站,外貿(mào)網(wǎng)站制作,寧陽(yáng)網(wǎng)站建設(shè)費(fèi)用合理。
一、清空元素的區(qū)別
1、錯(cuò)誤做法一:
$("#test").html("");//該做法會(huì)導(dǎo)致內(nèi)存泄露
2、錯(cuò)誤做法二:
$("#test")[0].innerHTML=""; ;//該做法會(huì)導(dǎo)致內(nèi)存泄露
3、正確做法:
//$("#test").empty();
二、原理:
在 jQuery 中用 innerHTML 的方法來(lái)清空元素,是必然會(huì)導(dǎo)致內(nèi)存泄露的,由于 jquery 對(duì)于同一元素多事件處理沒(méi)有直接采用瀏覽器事件模型,而是自己緩存事件,遍歷觸發(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)到了元素上面,上述事件即是采用該機(jī)制緩存事件監(jiān)聽(tīng)器。
那么就可以知道,直接 innerHTML=“” 而不通知 jquery 清空與將要?jiǎng)h除元素關(guān)聯(lián)的數(shù)據(jù),那么這部分?jǐn)?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)容,你們對(duì)清空元素html("") innerHTML="" 與 empty()的區(qū)別和應(yīng)用有哪些有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享標(biāo)題:清空元素html("")innerHTML=""與empty()的區(qū)別和應(yīng)用有哪些
文章位置:http://chinadenli.net/article2/gpcooc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、靜態(tài)網(wǎng)站、關(guān)鍵詞優(yōu)化、網(wǎng)站維護(hù)、移動(dòng)網(wǎng)站建設(shè)、軟件開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)