Remove all child elements of a DOM node in JavaScript

前端 未结 30 1856
花落未央
花落未央 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:08

    elm.replaceChildren()

    It's experimental without wide support, but when executed with no params will do what you're asking for, and it's more efficient than looping through each child and removing it. As mentioned already, replacing innerHTML with an empty string will require HTML parsing on the browser's part.

    Documentation here.

提交回复
热议问题