开发者

How do you render a jQuery object with underscore template

开发者 https://www.devze.com 2023-04-02 00:12 出处:网络
I was wondering how I can render a jQuery object with _.template. $(function(){ var $el = $(\'p\')开发者_开发知识库;

I was wondering how I can render a jQuery object with _.template.

$(function(){
    var $el = $('p')开发者_开发知识库;
    $el.html('Hello');

    var context = {
        'elem' : $el
    }

    var tmpl = $('#tmpl').html();
    var result = _.template(tmpl)(context);

    $('div').html(result);
});

http://jsfiddle.net/bDVeV/

This returns [object Object]. I read to get the outer html you can do $el[0].outerHTML but then I lose my click event it seems.


$el is an object, not a string, so you have to get the data into json format. This will work:

var context = {
        'elem' : $el.html()
    }

and don't forget to bind the click event after the p tag is inserted into the DOM

0

精彩评论

暂无评论...
验证码 换一张
取 消