How to insert a large block of HTML in JavaScript?

后端 未结 7 868
悲哀的现实
悲哀的现实 2020-12-07 18:50

If I have a block of HTML with many tags, how do insert it in JavaScript?

var div = document.createElement(\'div\');
div.setAttribute(\'class\', \'post block         


        
7条回答
  •  感动是毒
    2020-12-07 19:14

    Add each line of the code to a variable and then write the variable to your inner HTML. See below:

    var div = document.createElement('div');
    div.setAttribute('class', 'post block bc2');
    var str = "First Line";
    str += "Second Line";
    str += "So on, all of your lines";
    div.innerHTML = str;
    document.getElementById('posts').appendChild(div);
    

提交回复
热议问题