用户工具

站点工具


function removeAllChildNodes(parent) {
    while (parent.firstChild) {
        parent.removeChild(parent.firstChild);
    }
}
const container = document.querySelector('#container');
removeAllChildNodes(container);

使用以上函数清空子元素

parent.innerHTML = '';

也不是不行,但是不会清空事件 handler,可能造成内存泄漏

/opt/bitnami/dokuwiki/data/pages/javascript/清空子元素.txt · 最后更改: 2021/11/25 06:35 由 superuser