欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

清空元素html("") innerHTML="" 與 empty()的區(qū)別和應(yīng)用(推薦)

 更新時(shí)間:2017年08月14日 16:57:45   作者:逐浪_一生懸命  
這篇文章主要介紹了清空元素html("")、innerHTML="" 與 empty()的區(qū)別和應(yīng)用,詳細(xì)介紹了三者之間的原理及應(yīng)用,需要的朋友可以參考下

一、清空元素的區(qū)別

     1、錯(cuò)誤做法一:

           $("#test").html("");//該做法會導(dǎo)致內(nèi)存泄露

     2、錯(cuò)誤做法二:

           $("#test")[0].innerHTML="";  ;//該做法會導(dǎo)致內(nèi)存泄露

     3、正確做法:

        //$("#test").empty();       

二、原理:

在 jQuery 中用 innerHTML 的方法來清空元素,是必然會導(dǎo)致內(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)到了元素上面,上述事件即是采用該機(jī)制緩存事件監(jiān)聽器。

那么就可以知道,直接 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 ); 
 }

  以上就是小編為大家整理的清空元素html("")、innerHTML="" 與 empty()的區(qū)別和應(yīng)用的全部內(nèi)容啦~希望能夠幫助到各位朋友~~

相關(guān)文章

最新評論