我有一个以JSON格式返回的AJAX响应,它看起来像这样:
{
"color":"DEDE54",
"text":"<p>some text<\/p>"
}`
我希望将其以格式化的HTML形式附加。我现在只有一个字符串
comment
,并在应该存在的变量位置添加它们。这种方法能够实现,但看起来比较混乱,不易读懂。请问是否有更好的做法?
appendResponse:function (response) {
if (response !== 'wait'){
var color = response.color;
var text = response.text;
var comment = '<div class="comment"><div class="avatar" style="background:#'+color+'"></div><div class="text">'+text+'<div class="date">Just now</div></div></div>';
$('#comment-form').after(comment);
} else {
Materialize.toast('Wait 20 seconds between comments', 5000, 'toast-alert');
}
}