开发者

Proper way to use $.tmpl in jQuery

开发者 https://www.devze.com 2023-03-28 10:57 出处:网络
I\'m using tmpl in开发者_C百科 many places but I see a lot of repeats in my code like $(\'#sel\').html(\'\');// cleanup

I'm using tmpl in开发者_C百科 many places but I see a lot of repeats in my code like

$('#sel').html('');    // cleanup
$.tmpl('template', data).appendTo('#sel');

looks like I'm missing something, this should be possible with $.tmpl only. Any idea?


Why not $('#sel').html($.tmpl('template', data)) ?


In my view it is better to clean up first and then use

$.tmpl('template', data).appendTo('#sel');

Because jquery spec recommends it. Take a look at this piece of doc taken from http://api.jquery.com/jQuery.template/

To insert the rendered template items into the HTML DOM, the returned jQuery collection should not be inserted directly into the DOM, but should be chained with .appendTo, .prependTo, .insertAfter or .insertBefore

0

精彩评论

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