개인공부/TIL(Today I Learned)

TIL 27일차_DOM 모든 자식노드 삭제하기

soon327 2021. 2. 14. 00:36

Remove All children


모든 자식노드를 삭제하는 방법들

  1. const parent= document.querySelector(".parent");
    while (parent.firstChild) {
     parent.removeChild(parent.firstChild);
    }
  2. const parent= document.querySelector(".parent");
    while (parent.hasChildNodes()){
     parent.removeChild(parent.firstChild)};
  3. const parent= document.querySelector(".parent");
    parent.querySelectorAll('*').forEach(n => n.remove());