javascript innerHTML adding instead of replacing

前端 未结 6 1527
名媛妹妹
名媛妹妹 2020-11-30 04:32

quick question, i know we can change the content of a

hello one
by using:

docum         


        
6条回答
  •  眼角桃花
    2020-11-30 05:10

    If you are appending, you can just change your = to a +=

    document.getElementById("whatEverId").innerHTML += 'hello two';

    If prefixing

    document.getElementById("whatEverId").innerHTML = 'hello two' + document.getElementById("whatEverId").innerHTML;

    Although I would highly recommend using jQuery or MooTools javascript libraries/frameworks to do this sort of thing. If you're adding tags not just text nodes, then you should use the DOM createElement or one of the aforementioned libraries/frameworks.

提交回复
热议问题