0
我剛剛學習Javascript和jQuery。我做了一個基本上已經破解的簡單測驗,但是我已經將問題縮小到了我的代碼中。我確信所有的id都是鏈接的,並且jQuery與CDN正確連接。我知道我可以一個接一個地提出問題,但是這是潦草的編碼。jQuery簡單測驗不工作
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
基本上,應該在開始隱藏不要負荷,被點擊開始測驗圖像時的部分,沒有任何反應。當我試圖將問題縮小爲一行代碼時,部分代碼隨機工作,然後無法工作,但沒有明顯的相關性。
要添加代碼,請單擊工具欄上的'{}'按鈕,然後在你的代碼粘貼。我已經修復它。 – AstroCB
出於興趣,由於錯誤與缺少字符有關,您使用的是哪種文本編輯器?一個帶有代碼提示可能有益於避免將來的錯誤 –
Standard Notepad ++ – HipsterLeprechaun