我目前在我的jquery中遇到长内联html,以便动态生成长html片段。 例如:
var personalMessage = $("<div title='" + chatid + "' class='personalMessage'><div class='personalChatName'>" + fullname + "</div><div class='personalChatDialog'></div></div>")
$ContactsBar.prepend(personalMessage);
我想在我的personalMessage
中添加更多html,所以我开始认为jquery.tmpl对此非常完美,但是jQuery停止使用它,我不知道为什么,但是有什么新东西或替代品现在每个人都使用那更好吗?或者每个人还在使用jquery.tmpl吗?
答案 0 :(得分:1)
我刚刚遇到过这个:t.js
但是,如果您的需求相当有限,您可以使用简单的String
函数扩展template
:
String.prototype.template = function(obj) {
return this.replace(/\{\{([\w]+)\}\}/g, function(str, prop) {
return obj[prop];
});
};
alert('<div>my name is: {{last}}, {{first}}</div>'.template({first:'John', last:'Smith'}));
答案 1 :(得分:0)