最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
清空元素html("")innerHTML=""與empty()的區(qū)別和應(yīng)用有哪些

今天就跟大家聊聊有關(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è)資訊頻道,感謝大家的支持。


分享題目:清空元素html("")innerHTML=""與empty()的區(qū)別和應(yīng)用有哪些
網(wǎng)站鏈接:http://fisionsoft.com.cn/article/gpcooc.html