移除DOM節點

來源:互聯網
上載者:User

在IE中移除容器類節點,會引起記憶體泄露,最好是建立一個新的節點,比如div,然後將要刪除的節點放入這個div中,再將div的innerHTML清空。其它的直接removeChild就可以了。

 var removeNode = !+"\v1" ? function(){    var d;    return function(node){        if(node && node.tagName != 'BODY'){            d = d || document.createElement('DIV');            d.appendChild(node);            d.innerHTML = '';        }    }}() : function(node){    if(node && node.parentNode && node.tagName !=  'BODY'){        node.parentNode.removeChild(node);    }}

<meta content="text/html; charset=utf-8" http-equiv="Content-Type"><br /><style type="text/css"> #ddd { width:400px; height:200px; background:red; } #eee { margin:20px; width:300px; height:100px; background:blue; } </style> 要去除的父元素</p> 子項目 </p><p> <button type="button" onclick="remove('ddd')">去除ddd元素</button><br /><button type="button" onclick="check('ddd')">檢測ddd元素是否存在</button><br /><button type="button" onclick="remove('eee')">去除eee元素</button><br /><button type="button" onclick="check('eee')">檢測eee元素是否存在</button></p><p>

運行代碼

移除節點:

xxx.removeChild(obj) xxx是所刪除節點的父節點

xxx.removeNode(true) xxx是所要刪除的節點

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.