2014-04-14 31 views
0

我刚刚学习Javascript和jQuery。我做了一个基本上已经破解的简单测验,但是我已经将问题缩小到了我的代码中。我确信所有的id都是链接的,并且jQuery与CDN正确连接。我知道我可以一个接一个地提出问题,但是这是潦草的编码。jQuery简单测验不工作

http://pastebin.com/54JQwhAg

HTML: <!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="Utf-8"> 
     <title>The quiz to end all quizes</title> 
     <link rel="stylesheet" href="style.css"> 
     <script src="libs/jquery-1.11.0.min.js"></script> 
     <script src="script.js"></script> 
     <link rel="shortcut icon" href="favicon.ico" type="image/x-icon"> 
     <link rel="icon" href="favicon.ico" type="image/x-icon"> 
    </head> 
    <body> 
     <div id="main"> 
      <div id="header"> 
       <img src="images/logo.png"> 
       <h2>This page is an experiment to see if I can use JavaScript and jQuery to make a quiz game.</h2> 
       <p>Please answer with no punctuation.</p> 
       <p id="show">Click here to show answers.</p> 
       <img id="start" src="images/clickme.png"> 
      </div> 
      <div id="answers"> 
       <!-- CHEATER!!! --> 
       <p><span id="ans1"></span>Q: Who was the first African-American major league baseball player? A: Jackie Robinson</p> 
       <p><span id="ans2"></span>Q: Who was the second U.S. President? A: John Adams</p> 
       <p><span id="ans3"></span>Q: Where did the general keep his armies? A: In his sleevies</p> 
       <p><span id="ans4"></span>Q: Why did the chicken cross the road? A: To get to the other side</p> 
       <p><span id="ans5"></span>Q: Why did the scarecrow get a promotion? A: He was outstanding in his field</p> 
       <h2 id="numright"></h2> 
       <p id="hide">Click here to hide answers.</p> 
      </div> 
     </div> 
    </body> 
</html> 

Javascript: 
$(document).ready(function() { 
    var score; 
    var questions = [ 
        "Who was the first African-American major league baseball player?", 
        "Who was the second U.S. President?", 
        "Where did the general keep his armies?", 
        "Why did the chicken cross the road?", 
        "Why did the scarecrow get a promotion?" 
        ]; 
    var answers = [ 
        "jackie robinson", 
        "john adams", 
        "in his sleevies", 
        "to get to the other side", 
        "He was outstanding in his field" 
        ]; 
    $('#answers').hide() // hide answers 
    $('#start').click(function() { 
     for (num=0; num<5, num++){ 
      var ans = prompt(questions[num], "Enter Answer Here") 
      if (ans == answers[num]) 
       score++ 
      } // end if 
     } // end for 
     $('#answers').show() // show answers 
     $('#show').hide() // hide show 
     $('#numright').write('You got ' + score + '/5') 
    }); // end click 
    $("#show").click(function() { 
     $('#answers').show() // show answers 
     $('#show').hide() // hide show button 
    }); // end click 
    $("#hide").click(function() { 
     $('#answers').hide() // hide answers 
     $('#show').show() // show show button 
    }); // end click  
}); // end ready 

基本上,应该在开始隐藏不要负荷,被点击开始测验图像时的部分,没有任何反应。当我试图将问题缩小为一行代码时,部分代码随机工作,然后无法工作,但没有明显的相关性。

+1

要添加代码,请单击工具栏上的'{}'按钮,然后在你的代码粘贴。我已经修复它。 – AstroCB

+0

出于兴趣,由于错误与缺少字符有关,您使用的是哪种文本编辑器?一个带有代码提示可能有益于避免将来的错误 –

+0

Standard Notepad ++ – HipsterLeprechaun

回答

1

我发现要通过代码2级的错误,无论是在for循环

首先你的for循环有一个逗号而不是分号后num<5

其次,你失踪在你的大括号如果更正声明,

for (num=0; num<5; num++) { 
    var ans = prompt(questions[num], "Enter Answer Here") 
    if (ans == answers[num]) { 
     score++ 
    } // end if 
} // end for 

此外,有一对夫妇更多的事情需要注意的

在尝试增加变量score之前,您需要初始化它;

var score = 0; 

进一步回落,在<h2>标签输出的分数,当你使用.write()作为一个功能,我认为基于JavaScript的document.write()。在jQuery中使用.text()代替输出并成功输出。

Fixed Fiddle