准备html字符串
无论怎么插入,那堆html字符串肯定是省不了的……
所以你可以选择是把那堆html放在
然后用$('#tr-template').html()来获取。
还是直接作为Ajax的结果返回。
插入字符串
插入的话,用$(...).append(html)就能把字符串加入到目标元素中了。
不过貌似你还需要做一些替换(如果能直接插入就不成问题了对吧),还好有人写了个String.format函数
所以你可以拷下来改改,用这个String.format将模板中的{1},{2}什么的替换成Ajax返回的变量,这样再插入就好了。
更进一步说,还可以拓展这个函数,提高它的可读性:
if (!String.prototype.format) {
String.prototype.format = function() {
var hash = arguments[0];
return this.replace(/{(\w+)}/g, function(match, item) {
return typeof hash[item] != 'undefined' ? hash[item] : match;
});
};
}
console.log("my name is {name}".format({ name: '顺其自然' }));