2013-06-18 145 views
0

这行代码($(“#测验模板”)的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)); 
    } 
}; 
+0

您确定该脚本是在元素加载后执行的吗? –

+0

在浏览器控制台,您可以试试'$(“#竞猜模板”)。HTML()' –

+0

我这样做是在调试时,它返回undefined。但我不知道为什么 –

回答

0

确保在文档加载您SHOWPAGE功能执行。你检查过了吗?

编辑: 我想你不能得到,因为“脚本”标签的HTML的原因。我会推荐使用以下任何一种:

1) Simple: 
<textarea id="name" style="display:none"> 
     ... templates ... 
</textarea> 


2) Valid XHTML 1.1 (using CDATA): 
<p style="display:none"><textarea id="name" rows="0" cols="0"><![CDATA[ 
     ... templates ... 
]]></textarea></p> 


3) Valid XHTML 1.1 (using comments; suggested): 
<p style="display:none"><textarea id="name" rows="0" cols="0"><!-- 
     ... templates ... 
--></textarea></p> 
+0

是没有元素。它被封装在$(文件)。就绪() –

+0

谢谢你的帮助。我尝试了他们,不幸的是他们都没有工作... –