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

后端 未结 7 1394
天涯浪人
天涯浪人 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:44

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

    
           ....................
           
           
    
    
      //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.

    
    

    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.

提交回复
热议问题