how to assign a block of html code to a javascript variable

后端 未结 7 1380
天涯浪人
天涯浪人 2020-12-04 18:02

what is the syntax to store a block of html code to a javascript variable?

test.test
相关标签:
7条回答
  • 2020-12-04 18:39

    we can use backticks (``) without any error.. eg: <div>"test"<div>

    we can store large template(HTML) inside the backticks which was introduced in ES6 javascript standard

    No need to escape any special characters

    if no backticks.. we need to escape characters by appending backslash() eg:" \"test\""

    0 讨论(0)
  • 2020-12-04 18:41

    Greetings! I know this is an older post, but I found it through Google when searching for "javascript add large block of html as variable". I thought I'd post an alternate solution.

    First, I'd recommend using single-quotes around the variable itself ... makes it easier to preserve double-quotes in the actual HTML code.

    You can use a backslash to separate lines if you want to maintain a sense of formatting to the code:

    var code = '<div class="my-class"> \
            <h1>The Header</h1> \
            <p>The paragraph of text</p> \
            <div class="my-quote"> \
                <p>The quote I\'d like to put in a div</p> \
            </div> \
        </div>';
    

    Note: You'll obviously need to escape any single-quotes inside the code (e.g. inside the last 'p' tag)

    Anyway, I hope that helps someone else that may be looking for the same answer I was ... Cheers!

    0 讨论(0)
  • 2020-12-04 18:41

    Just for reference, here is a benchmark of different technique rendering performances,

    http://jsperf.com/zp-string-concatenation/6

    m,

    0 讨论(0)
  • 2020-12-04 18:42
     var test = "<div class='saved' >"+
     "<div >test.test</div> <div class='remove'>[Remove]</div></div>";
    

    You can add "\n" if you require line-break.

    0 讨论(0)
  • 2020-12-04 18:44

    I recommend to use mustache templating frame work. https://github.com/janl/mustache.js/.

    <body>
           ....................
           <!--Put your html variable in a script and set the type to "x-tmpl-mustache"-->
           <script id="template" type="x-tmpl-mustache">
               <div class='saved' >
               <div >test.test</div> <div class='remove'>[Remove]</div></div>
        </script>
    </body>
    
      //You can use it without jquery.
      var template = $('#template').html();
      var rendered = Mustache.render(template);
      $('#target').html(rendered);
    

    Why I recommend this?

    Soon or latter you will try to replace some part of the HTML variable and make it dynamic. Dealing with this as an HTML String will be a headache. Here is where Mustache magic can help you.

    <script id="template" type="x-tmpl-mustache">
     <div class='remove'>  {{ name }}! </div> ....
    </script>
    

    and

     var template = $('#template').html();
     // You can pass dynamic template values
      var rendered = Mustache.render(template, {name: "Luke"});
      $('#target').html(rendered);
    

    There are lot more features.

    0 讨论(0)
  • 2020-12-04 18:50

    Modern Javascript implementations with the template syntax using backticks are also an easy way to assign an HTML block of code to a variable:

        const firstName = 'Sam';
        const fullName = 'Sam Smith';
        const htmlString = `<h1>Hello ${fullName}!</h1><p>This is some content \
            that will display. You can even inject your first name, ${firstName}, \
            in the code.</p><p><a href="http://www.google.com">Search</a> for \
            stuff on the Google website.</p>`;
    
    0 讨论(0)
提交回复
热议问题