How to insert a large block of HTML in JavaScript?

后端 未结 7 839
悲哀的现实
悲哀的现实 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 18:58

    The easiest way to insert html blocks is to use template strings (backticks). It will also allow you to insert dynamic content via ${...}:

    document.getElementById("log-menu").innerHTML = `
                <a href="#"> 
                   ${data.user.email}
                </a>
                <div class="dropdown-menu p-3 dropdown-menu-right">
                    <div class="form-group">
                        <label for="email1">Logged in as:</label>
                        <p>${data.user.email}</p>
                    </div>
                    <button onClick="getLogout()" ">Sign out</button>
                </div>
        `
    
    0 讨论(0)
  • 2020-12-07 19:01

    Template literals may solve your issue as it will allow writing multi-line strings and string interpolation features. You can use variables or expression inside string (as given below). It's easy to insert bulk html in a reader friendly way.

    I have modified the example given in question and please see it below. I am not sure how much browser compatible Template literals are. Please read about Template literals here.

    var a = 1, b = 2;
    var div = document.createElement('div');
    div.setAttribute('class', 'post block bc2');
    div.innerHTML = `
        <div class="parent">
            <div class="child">${a}</div>
            <div class="child">+</div>
            <div class="child">${b}</div>
            <div class="child">=</div>
            <div class="child">${a + b}</div>
        </div>
    `;
    document.getElementById('posts').appendChild(div);
    .parent {
      background-color: blue;
      display: flex;
      justify-content: center;
    }
    .post div {
      color: white;
      font-size: 2.5em;
      padding: 20px;
    }
    <div id="posts"></div>

    0 讨论(0)
  • 2020-12-07 19:10

    Despite the imprecise nature of the question, here's my interpretive answer.

    var html = [
        '<div> A line</div>',
        '<div> Add more lines</div>',
        '<div> To the array as you need.</div>'
    ].join('');
    
    var div = document.createElement('div');
        div.setAttribute('class', 'post block bc2');
        div.innerHTML = html;
        document.getElementById('posts').appendChild(div);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-07 19:15

    If you are using on the same domain then you can create a seperate HTML file and then import this using the code from this answer by @Stano :

    https://stackoverflow.com/a/34579496/2468603

    0 讨论(0)
  • 2020-12-07 19:18

    If I understand correctly, you're looking for a multi-line representation, for readability? You want something like a here-string in other languages. Javascript can come close with this:

    var x =
        "<div> \
        <span> \
        <p> \
        some text \
        </p> \
        </div>";
    
    0 讨论(0)
提交回复
热议问题