假設下面的標記:最安全的,與兒童
<div id="outterParent">
<div id="innerParent">
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
</div>
</div>
在安全性(避免內存泄漏)和性能方面,是確定做這樣的事情:
var outterParent = document.getElementById("outterParent");
var innerParent = document.getElementById("innerParent");
outterParent.removeChild(innerParent);
outterParent = innerParent = null;
...或者是它更好地去除#innerParent
之前每個.children
元件移除,如下所示:
var outterParent = document.getElementById("outterParent");
var innerParent = document.getElementById("innerParent");
var child;
while (innerParent.firstChild){
child = innerParent.firstChild;
innerParent.removeChild(child);
}
outterParent.removeChild(innerParent);
outterParent = innerParent = child = null;
您的瀏覽器有開發工具(廓線),您可以衡量這一點。例如。 https://developers.google.com/web/tools/chrome-devtools/profile/?hl=zh-CN –
@FelixKling我正在爲iOS開發,所以Chrome並沒有真正幫助我,但是,那裏_must_是一個最佳做法。 – West1
Safari也有開發者工具;) –