所以我有一个html模板:
<div style='background-color: red;'>
<div>
$QuestionUpButton
<%--<img style="cursor: pointer" onclick="onAddQUestionVote" src="../../Images/arrow-up-24-ns.png" data-questionvoteupbutton="$i" id='addQuestionVote' alt='Vote up' data-questionid='$questionUid' />--%>
<span data-questionvotegroup='$i'>$QuestionVote</span>
<span data-questiontextgroup='$i'>$textboxText<br />
</span>
</div>
<div id="mm" style='background-color: white;'>
$AnswerUpVote
<%--<img style="cursor: pointer" onclick="onAddAnswerVote" src="../../Images/arrow-up-24-ns.png" data-answervoteupbutton="$i" id='addAnswerVote' alt='Vote up' data-answeruid='$answerGuid' />--%>
<img style="cursor: pointer" onclick="onRemoveAnswerVote" src="../../Images/arrow-down-24-ns.png" data-answervotedownbutton="$i" id='removeAnswerVote' alt='Vote down' data-answeruid='$answerGuid' />
<span data-answervotegroup='$i'>$AnswerVotes</span>
<span data-labelgroup='$i'>Answer : </span>
<span data-answertextgroup="$i" data-questionid='$questionUid' data-answerid="$answerGuid">$AnswerSpan</span>
<input type='button' id='editButton' data-editlinkbuttongroup="$i" value='Edit' />
<input type='button' id='deleteButton' data-deletelinkbuttongroup="$i" value='Delete Question' />
</div>
</div>
在文档加载时,我运行代码替换所有 $ 变量,如下所示:
function MyFunction(result) {
//console.log(result);
var panel = $('#mypanel');
var userGuid = GetUserGuid().d;
var panel = $('#MainContent_AnsweredQuestions_mypanel');
for (var k = 0; k < result.d.length; k++) {
var notVotedQuestion = "<img style='cursor: pointer' onclick='onAddQUestionVote' src='../../Images/arrow-up-24-ns.png' data-questionvoteupbutton='" + i + "' id='Img1' alt='Vote up' data-questionid='" + result.d[k].Uid + "' />";
var notVotedQuestion1 = "hulabula";
var notVotedAnswer = "notVotedAnswer";
var votedAnswer = "";
console.log(result.d[k].Uid)
var html = $("#OwnerTemplateQuestionWithoutAnswer").html().replace(/\$i/g, i)
.replace(/\$AnswerSpan/g, result.d[k].Answers[0].Conetent)
.replace(/\$answerGuid/g, result.d[k].Answers[0].Uid)
.replace(/\$questionUid/g, result.d[k].Uid)
.replace(/\$QuestionVote/g, result.d[k].QuestionVotes.length)
.replace(/\$AnswerVotes/g, result.d[k].Answers[0].AnswerVotes.length)
.replace(/\$textboxText/g, result.d[k].Content);
//var html1 = $("#OwnerTemplateQuestionWithoutAnswer").html(html);
if (didUserVote(result.d[k], userGuid) == false){
html.html().replace("$AnswerUpVote", notVotedQuestion);
}
panel.append(html);
i++;
}
}
我第一次更换的部分工作得很好,正是我想要的:
var html = $("#OwnerTemplateQuestionWithoutAnswer").html().replace(/\$i/g, i)
.replace(/\$AnswerSpan/g, result.d[k].Answers[0].Conetent)
.replace(/\$answerGuid/g, result.d[k].Answers[0].Uid)
.replace(/\$questionUid/g, result.d[k].Uid)
.replace(/\$QuestionVote/g, result.d[k].QuestionVotes.length)
.replace(/\$AnswerVotes/g, result.d[k].Answers[0].AnswerVotes.length)
.replace(/\$textboxText/g, result.d[k].Content);
但稍后在代码中我需要以不同方式加载图像取决于某些条件
if (didUserVote(result.d[k], userGuid) == false){
html.html().replace("$AnswerUpVote", notVotedQuestion);
}
而这部分 cide 根本不起作用......关于如何解决这个问题的任何建议,或者我如何以某种方式改进替换?