How can I remove a child node in HTML using JavaScript?

前端 未结 9 1393
野的像风
野的像风 2020-12-12 20:22

Is there a function like document.getElementById(\"FirstDiv\").clear()?

相关标签:
9条回答
  • 2020-12-12 20:57

    You should probably use a JavaScript library to do things like this.

    For example, MochiKit has a function removeElement, and jQuery has remove.

    0 讨论(0)
  • 2020-12-12 21:07

    You should be able to use the .RemoveNode method of the node or the .RemoveChild method of the parent node.

    0 讨论(0)
  • 2020-12-12 21:08

    Modern Solution - child.remove()

    For your use case:

    document.getElementById("FirstDiv").remove()
    

    This is recommended by W3C since late 2015, and is vanilla JS. All major browsers support it.

    Mozilla Docs

    Supported Browsers - 96% May 2020

    0 讨论(0)
提交回复
热议问题