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
精彩评论