Javascript使用技巧
Template.Util = {//tool type format : function (str, model) {//format the template for (var k in model) { var re = new RegExp("{" + k + "}", "g"); str = str.replace(re, model[k]) } return str; } }
使用方式:
var Template = { fn: { constant: { tab: '<table id={tableId} border=0 cellpadding=1 cellspacing=1 class=tb_datalist></table>' } } } createHoriz: function(item) { return $(Template.Util.format(Vc.constant.horizontal, { "ULId": item.ULId })); }
好处是构造$jquery对象可以直接用字符串+json对象进行替换,代码清晰