spread html in multiple lines javascript

前端 未结 4 1680
轻奢々
轻奢々 2021-02-05 21:09

I want to put the below html code in my javascript function. I don\'t want to put it all next to each other. Is it possible to the code in the same way as how it is in html? Cod

相关标签:
4条回答
  • 2021-02-05 21:26

    You can kind of do this:

    var myHtml = [
     'firstline',
     'second line',
     'third line'].join("\n");
    

    So pretty much you still need to break up the code some how.

    An alternative (if the code is going to be large) would be to store in a HTML file and when you need, retrieve it quickly with $.get and possibly cache it somewhere in a variable.

    $.get('snippet.html', function(data) {
      $('.result').html(data);
    });
    

    I think this is actually a pretty good idea as modifying that snippet will be much easier.

    0 讨论(0)
  • 2021-02-05 21:29

    Updated, seems like Javascript does support multi lines by adding a backslash ( \ ) as the last character in the line, you can't even have a space bar behind it, because then it won't cancel the line break.

        var str = '<div data-role="page" data-add-back-btn="true" id="Gallery2" class="gallery-page"> \
        <div data-role="header">  \
            <h1>Second Gallery</h1> \
        </div> \
        <div data-role="content"> \  
            <ul class="gallery"> \
                <li><a href="images/full/010.jpg" rel="external"><img src="images/thumb/010.jpg" alt="Image 010" /></a></li> \
                <li><a href="images/full/011.jpg" rel="external"><img src="images/thumb/011.jpg" alt="Image 011" /></a></li> \
                <li><a href="images/full/012.jpg" rel="external"><img src="images/thumb/012.jpg" alt="Image 012" /></a></li> \
                <li><a href="images/full/013.jpg" rel="external"><img src="images/thumb/013.jpg" alt="Image 013" /></a></li> \
                <li><a href="images/full/014.jpg" rel="external"><img src="images/thumb/014.jpg" alt="Image 014" /></a></li> \
                <li><a href="images/full/015.jpg" rel="external"><img src="images/thumb/015.jpg" alt="Image 015" /></a></li> \
                <li><a href="images/full/016.jpg" rel="external"><img src="images/thumb/016.jpg" alt="Image 016" /></a></li> \
                <li><a href="images/full/017.jpg" rel="external"><img src="images/thumb/017.jpg" alt="Image 017" /></a></li> \
                <li><a href="images/full/018.jpg" rel="external"><img src="images/thumb/018.jpg" alt="Image 018" /></a></li> \
            </ul> \
        </div> \
        <div data-role="footer"> \
            <h4>&copy; 2011 Code Computerlove</h4> \
        </div> \
    </div>';
    

    Or use a HTML template and load it into a DIV (using jQuery)

    $("#div").load("/html_template.html");
    
    0 讨论(0)
  • 2021-02-05 21:38

    Really old question, but I don't find this syntax a lot, so I'll just leave this here, as to me it is the most readable.

    var html = '' +
        '    <div data-role="page" data-add-back-btn="true" id="Gallery2" class="gallery-page">' +
        '' +
        '        <div data-role="header">' +
        '            <h1>Second Gallery</h1>' +
        '        </div>' +
        '' +
        '        <div data-role="content">   ' +
        '' +
        '            <ul class="gallery">' +
        '' +
        '                <li><a href="images/full/010.jpg" rel="external"><img src="images/thumb/010.jpg" alt="Image 010" /></a></li>' +
        '                <li><a href="images/full/011.jpg" rel="external"><img src="images/thumb/011.jpg" alt="Image 011" /></a></li>' +
        '                <li><a href="images/full/012.jpg" rel="external"><img src="images/thumb/012.jpg" alt="Image 012" /></a></li>' +
        '                <li><a href="images/full/013.jpg" rel="external"><img src="images/thumb/013.jpg" alt="Image 013" /></a></li>' +
        '                <li><a href="images/full/014.jpg" rel="external"><img src="images/thumb/014.jpg" alt="Image 014" /></a></li>' +
        '                <li><a href="images/full/015.jpg" rel="external"><img src="images/thumb/015.jpg" alt="Image 015" /></a></li>' +
        '                <li><a href="images/full/016.jpg" rel="external"><img src="images/thumb/016.jpg" alt="Image 016" /></a></li>' +
        '                <li><a href="images/full/017.jpg" rel="external"><img src="images/thumb/017.jpg" alt="Image 017" /></a></li>' +
        '                <li><a href="images/full/018.jpg" rel="external"><img src="images/thumb/018.jpg" alt="Image 018" /></a></li>' +
        '' +
        '            </ul>' +
        '' +
        '        </div>' +
        '' +
        '        <div data-role="footer">' +
        '            <h4>&copy; 2011 Code Computerlove</h4>' +
        '        </div>' +
        '' +
        '    </div>';
    

    With a simple shell command to generate this from an html file:

    cat myfile.html | sed "s/'/\\'/g" | sed "$ ! s/^.*$/'\0' +/g" | sed "$ s/^.*$/'\0';/"
    

    Update: With ES6, simply use backticks:

    const html = `
      <div>
        <!-- Content -->
      </div>
    `;
    
    0 讨论(0)
  • 2021-02-05 21:40

    Generating html from javascript directly ties logic and presentation together. It also garbles your code.

    You can use a javascript templating engine like pure to get a clear separation of logic and presentation.

    0 讨论(0)
提交回复
热议问题