`

javascript - trick/guideline to remove DOM element

阅读更多

remove an element is not as simple as calling parent.RemoveChild, housekeeping include 

 

  1. remove element should clean the handler that is associated with the element, otherwise, IE will leak memory should the function reference an external DOM element
  2. remove the data associated with the element
  3. remove should happen on the element (the parent) and all its descendant

and there is a caveat on IE, where most browsers will reclaim the memory happily, IE does not reclaim the memory that is occupied by the removed elements until the page is left.  


I am giving a pseudo-code as follow to show you you would normally write the remove function if  you ever need to do so. 


/**
* @Comment: this shows you how to remove DOM element from the tree, it worth the discussion because  
*   1. remove element should clean the handler that is associated with the element, otherwise, IE will leak memory should the function reference an external DOM element
*   2. remove the data associated with the element
*   3. remove should happen on the element (the parent) and all its descendant
* one caveat abou the remove, after the removal of the element, browsers may have different strategy to reclaim the memory it used, IE is an exception where it fail to reclaim 
* the memory until the page is finally left. 
* 
* so, for IE,an additional step is to set the outerHTML = "" (a trick) , which will wipe out elment from IE's memory more completely than simply doing removing child. 
*
* NOTE: this code shows example, and is not supposed to be useful without context.
*/
function remove() {
  // go through all descendants and the element to be removed.
  jQuery("*", this).add([this]).each(function () {
    // remove all bound events 
    jQuery.event.remove(this);
    // remove attached data
    jQuery.removeData(this);
  });
  // remove the element (if it 's in the DOM)
 
  if (this.parentNode) {
    this.parentNode.removeChild(this);

  // if it is IE, you will need to set the outerHTML as well
    if (typeof this.outerHTML !== "undefined")
      this.outerHTML = "";
  
  }
}
 
分享到:
评论

相关推荐

    python-trick

    Python-trick,上传的事pdf文档

    OutlookAttachView v2.73

    o Added %from_email% and %to_email% variables to the attachment filename format. * Version 2.71 o Added %localmsgtime% and %localmsgdate% variables to the attachment filename format. o Fixed a ...

    S-a-D-trick.rar_Join In

    split file and join write in vb6

    深度学习领域CNN橄榄球比赛NFL目标检测(带数据集)-cnn-baseline-more-tta-trick

    语言:python 内容包括:源码、数据集、数据集描述、论文 目的:使用CNN算法在橄榄球比赛中目标检测。 带数据集很好运行,主页有搭建环境过程。主页有更多源码。 数据集描述如下: 在这场比赛中,你的任务是预测球员...

    ht-history:从 code.google.compht-history 自动导出

    ht-历史为在线足球经理 hattrick.org 提供多项统计数据版本历史: 添加, 移除,C 改变,v 0.1.24 - 2015 年 1 月 16 日; 下载: : C 允许负 id(几乎)无处不在; 从而修复溢出错误v 0.1.23 - 09/04/2014; 下载:...

    tips-n-trick

    概述 这是Tips-N-Trick,书签等面向公众的文档。

    gtg-grind-trick-generator

    gtg-grind-trick-generator PWA Web应用程序(Node.js,JS,HTML,CSS) Chrome,Safari,Firefox,Edge(Android,iOS,MacOS,Windows) 离线工作Android应用程式使用Google Workbox,Webpack制作

    FoxTrick是一个浏览器扩展的帽子戏法在线足球经理游戏,目前可用于Firefox,谷歌Chrome,以及Opera。- minj / foxtrick

    Foxtrick enhances the user experience on Hattrick by providing tons of extra information, a lot of shortcuts, tweaks for the presentational layout of the web pages, analyses of matches, links to ...

    py-trick-book:关于 Python 的高级提示和技巧

    Python 提示和技巧 ... git clone https://github.com/plasmashadow/py-trick-book.git 我假设您安装了 ipython 导航到目录并执行 ipython notebook 笔记: 退出间谍活动并自己阅读 或者 保持冷静,加入草帽海贼团

    chinese-nlp-ner-master_chinese_BLSTM_

    一套针对中文实体识别的BLSTM-CRF解决方案,主要包括:数据处理,模型构建,模型训练,模型测试,服务部署(thrift和flask)两种方式。

    21-card-trick:在 React 中完成的 21 张卡片技巧

    React 21 卡技巧一个演示卡片技巧的React应用程序。动机该项目旨在学习如何使用 React 钩子和进行嵌套的 api 调用。 该项目不再进行。怎么玩记住 21 张卡片中的 1 张后,单击完成。 选择您的卡片所在的 3 堆中的哪一...

    HLP-Trick-crx插件

    语言:English (United States) 该扩展程序重新启用了网页上的复制/粘贴功能,上下文菜单和本机突出显示功能。

    CMCC-Trick:招惹CMCC-* WLAN

    Trick-CMCC 利用CMCC公共热点的小漏洞免费上网~~ :) sudo ./conn.sh Notice: 目前只知道我工CMCC有这特色, 其他地区尚不明确 Notice: 脚本适用于使用NetWorkManager网络sds管理工具的系统 Notice: 不必惊讶原理, ...

    HLP-绝招「HLP-Trick」-crx插件

    该扩展程序重新启用网页上的复制/粘贴功能,上下文菜单和本机突出显示功能。 支持语言:English (United States)

    Trick

    Trick

    mazu-sim:航空航天6自由度仿真平台

    妈祖心 妈祖模拟(mazu-sim)是6DoF火箭模拟平台。 它基于 目的是提供验证飞行软件、交付精度的硬件在环和飞行测试。 入门 0. 克隆回购 ...1. 依赖 GCC 5.4以上 ... $ sudo apt-get install htop cmake vim tree git-...

    ego-ui:使用自我图对Google自动完成结果进行交互式可视化

    受此文章启发: ://medium.com/applied-data-science/the-google-vs-trick-618c8fd5359f 这是前端部分,可在此处使用图形服务: : 用法: Click节点以突出显示其连接, Shift+Click可视化到中心节点的最短路径...

    ez-ipupdate动态域名解析

    我以前开发产品的动态域名解析 非常稳定 /* * ez-ipupdate * * a very simple dynDNS client for ... * I don't like to see UNIX get the short end of the stick. * * tested under Linux and Solaris. * */

    vdr-hattrick-开源

    vdr-hattrick是视频磁盘录像机(VDR)的插件。 它显示了在线足球游戏“ hattrick”(http://www.hattrick.org/)提供的实时比赛。

    JavaScript 有用的代码片段和 trick

    浮点数取整 const x = 123.4545; x >> 0; // 123 ~~x; // 123 x | 0; // 123 ...注意:前三种方法只适用于32个位整数,对于负数的处理上和 Math.floor是不同的。...Math.random().toString().slice

Global site tag (gtag.js) - Google Analytics