How to add content to html body using JS?

后端 未结 8 2045
孤城傲影
孤城傲影 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:00

    In most browsers, you can use a javascript variable instead of using document.getElementById. Say your html body content is like this:

    Hello

    Then you can just refer to mySection as a variable in javascript:

    mySection.innerText += ', world'
    // same as: document.getElementById('mySection').innerText += ', world'
    

    See this snippet:

    mySection.innerText += ', world!'
    Hello

提交回复
热议问题