Remove all child elements of a DOM node in JavaScript

前端 未结 30 1395
花落未央
花落未央 2020-11-22 03:28

How would I go about removing all of the child elements of a DOM node in JavaScript?

Say I have the following (ugly) HTML:

&

30条回答
  •  花落未央
    2020-11-22 04:17

    Why aren't we following the simplest method here "remove" looped inside while.

    const foo = document.querySelector(".foo");
    while (foo.firstChild) {
      foo.firstChild.remove();     
    }
    
    • Selecting the parent div
    • Using "remove" Method inside a While loop for eliminating First child element , until there is none left.

提交回复
热议问题