这行代码($(“#测验模板”)的html();)总是尽管一个脚本元素的存在与ID“测验模板”返回未定义。我不知道这个问题会是什么。你们有没有经历过这样的事情?任何帮助,将不胜感激。
的index.html:
<script id="quiz-template" type="text/html">
<h1>{{question}}</h1>
<hr>
<p class="right" style="display: none;">Richtig!</p>
<p class="wrong" style="display: none;">{{explanation}}</p>
<form id="answerForm" action="javascript:submitValue();">
{{#possibleAnswers}}
<div><label><input type="radio" name="answer" value="{{.}}">{{.}}</label></div>
{{/possibleAnswers}}
<input type="submit" value="Antwort bestätigen">
</form>
</script>
app.js:
showPage = function(pageIndex) {
currentPageIndex = pageIndex;
if (currentPageIndex == 0) {
template = $('#start-template').html();
console.log($('#start-template').html())
$('#content').html(Mustache.render(template));
}
else if (currentPageIndex == 11) {
template = $('#end-template').html();
$('#content').html(Mustache.render(template), tally);
}
else {
template = $('#quiz-template').html(); // this returns undefined
console.log($('#quiz-template').html());
question = pickRandomQuestion();
$('#content').html(Mustache.render(template, question));
}
};
您确定该脚本是在元素加载后执行的吗? –
在浏览器控制台,您可以试试'$(“#竞猜模板”)。HTML()' –
我这样做是在调试时,它返回undefined。但我不知道为什么 –