DOM
// innerHTML을 공백으로 설정, 파서 호출 -> 성능면에서 안 좋음
function remove_children() {
// 부모 노드 선택
const parent = document.getElementById('parent');
// 자식 노드 삭제
parent.innerHTML = "";
}
// textContent(부모와 자식의 text콘텐츠)를 공백으로 설정
function remove_children() {
const parent = document.getElementById('parent');
parent.textContent = "";
}
//replace 파라미터가 비어있으면 자식 전부 삭제
function remove_children() {
const parent = document.getElementById('parent');
parent.replaceChildren();
}
// removeChild 전달받은 자식요소 삭제 -> 자식 없을 때까지 반복
function remove_children() {
const parent = document.getElementById('parent');
while(parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}
//remove 자신 삭제 -> 자식 없을 때까지 반복
function remove_children() {
const parent = document.getElementById('parent');
while(parent.firstChild) {
parent.firstChild.remove()
}
}
https://hianna.tistory.com/722
JQuery
$(this).empty(); //자식요소 전부 삭제
$(this).remove(); //자신포함 전부 삭제
$(this).children().remove(); //자신포함 전부 삭제