How to add content to html body using JS?

后端 未结 8 2053
孤城傲影
孤城傲影 2020-11-28 07:19

I have many

in my html and want to add more through javascript. However, using innerHTML is just replacin
8条回答
  •  日久生厌
    2020-11-28 08:17

    You're probably using

    document.getElementById('element').innerHTML = "New content"
    

    Try this instead:

    document.getElementById('element').innerHTML += "New content"
    

    Or, preferably, use DOM Manipulation:

    document.getElementById('element').appendChild(document.createElement("div"))
    

    Dom manipulation would be preferred compared to using innerHTML, because innerHTML simply dumps a string into the document. The browser will have to reparse the entire document to get it's stucture.

提交回复
热议问题