Defining a HTML template to append using JQuery

前端 未结 7 1017
死守一世寂寞
死守一世寂寞 2020-11-29 14:48

I have got an array which I am looping through. Every time a condition is true, I want to append a copy of the HTML code below to a container element with some

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 15:25

    You could decide to make use of a templating engine in your project, such as:

    • mustache
    • underscore.js
    • handlebars

    If you don't want to include another library, John Resig offers a jQuery solution, similar to the one below.


    Browsers and screen readers ignore unrecognized script types:

    
    

    Using jQuery, adding rows based on the template would resemble:

    var template = $('#hidden-template').html();
    
    $('button.addRow').click(function() {
        $('#targetTable').append(template);
    });
    

提交回复
热议问题