这让我发疯:
// Get the comment template
var comment = $('#CommentTemplate').html();
// Delete all entered text
$("textarea#NewComment").val('');
// Get comments returned ID
var ThisCommentsID = data.substring(2, data.length);
var d = new Date();
// Replace comment variables
comment = comment.replace("{CID}", ThisCommentsID);
comment = comment.replace("{USERNAME}", CurrUsername);
comment = comment.replace("{BODY}", Message);
comment = comment.replace("{GRAV}", GravHash);
comment = comment.replace("{CLEARDATE}", GravHash);
comment = comment.replace("{ISODATE}", ISODateString(d));
comment = comment.replace("{USERURL}", RelURL + CurrUsername);
// Add it
$('#comment-total-wrap').prepend(comment);
这基本上可以100%正常工作,并通过AJAX查询向列表中动态添加评论,但是模板上的所有值仍以{CID},{GRAV}等形式出现。这不会引发任何错误,替换不是没有工作。
我试过了:
comment = comment.replace("{BODY}", Message);
同样,但它仍然在输出HTML上显示为{BODY}。任何帮助表示赞赏。
该页面上的HTML模板为:
<div id="CommentTemplate" style="display:none;">
<div id="cid{CID}" class="comment-wrapper">
<div id="CommentHead" class="comment-head ch-highlight">
<div class="comment-date">
<abbr class="timeago" title="{ISODATE}" id="Timeago{CID}">{CLEARDATE}</abbr>
</div>
<div class="comment-author">
Written by <a id="CommentAuthorLink" title="Visit this game makers profile" href="{USERURL}">{USERNAME}</a>
</div>
</div>
<table class="comment-body" width="100%">
<tr>
<td width="100" valign="top" align="center">
<a id="GravLink" title="{USERNAME} makes games with Construct 2" href="{USERURL}"><img id="GravatarComment" title="{USERNAME}'s Gravatar" class="comment-avatar" src="http://www.gravatar.com/avatar/{GRAV}?r=pg&s=80" /></a>
</td>
<td valign="top">
<div class="comment-txt">
{BODY}
</div>
</td>
</tr>
</table>
<div class="clear"></div>
</div>
</div>
最佳答案
你可以试试这个吗
comment.replace("{CID}", ThisCommentsID);
try this
comment= comment.replace(/\{CID\}/gi, ThisCommentsID);