测试 removeChild 导致的内存泄漏 产生内存泄漏方式 不产生内存泄漏方式 <script> <br>var dialog; <br>function add() <br>{ <br>dialog = document.createElement('div'); <br>var html = '<div><p>Title</p></div>'; <br>dialog.innerHTML = html; <br>document.body.appendChild(dialog); <br>dialog.style.marginTop='200px'; <br>dialog.style.marginLeft='200px'; <br>} <br>function remove() <br>{ <br>document.body.removeChild(dialog); <br>dialog=null; <br>} <br>function leak() <br>{ <br>for(var i=0;i<100000;i++){ <br>add(); <br>remove(); <br>} <br>alert('leak done'); <br>} <br>function notLeak() <br>{ <br>for(var i=0;i<100000;i++){ <br>add(); <br>discardElement(dialog); <br>} <br>alert('notLeak done'); <br>} <br>function discardElement(element) { <br>var garbageBin = document.getElementById('IELeakGarbageBin'); <br>if (!garbageBin) { <br>garbageBin = document.createElement('DIV'); <br>garbageBin.id = 'IELeakGarbageBin'; <br>garbageBin.style.display = 'none'; <br>document.body.appendChild(garbageBin); <br>} <br>// move the element to the garbage bin <br>garbageBin.appendChild(element); <br>garbageBin.innerHTML = ''; <br>} <br></script>