correct[]
とwrong[]
という2つの関数からのフィードバックをユーザーが質問に応じて表示しようとしています。私は、jQuery ready
関数を追加して、プロンプトの前に表示したいがすべて成功させるようにしようとしましたが、成功しませんでした。私は準備関数を自分のコードに何度も書いたことがあります。誰でも助けることができますか?ページがレンダリングされた後のJavascript/Jqueryスクリプトの実行
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<meta charset="UTF-8">
<title>Simple Math Quiz</title>
<script>
$("document").ready(function() {
$("body").css("background-color", "green");
});
</script>
</head>
<body>
<br><br>
<p><em><strong>Feedback</strong></em></p><p><br><br>
<script>
//Question array \t \t \t
\t \t \t var question = ["1. What is 1+1?",
"2. What is 2+2?",
"3. What is 3+3?",
"4. What is 4+4?",
\t \t \t \t \t \t \t \t "5. What is 5+5?"
];
//Other Variables
\t \t \t var qlength = question.length;
\t \t \t var counter = 0;
var answer = ["2", "4", "6", "8", "10"];
//First box to tell the viewer whats going on
\t \t \t alert('Answer the following 5 questions to determine if you are 1st grade smart.');
//Loop that asks the questions
\t for (var i = 0; i < qlength; i++)
\t \t \t \t {
\t \t \t \t \t var userAnswer = prompt(question[i]); //Stores the answer to each question in userAnswer
\t \t \t \t \t \t //Actions for correct answer
\t \t \t \t \t \t if (userAnswer == answer[i])
\t \t \t \t \t \t {
\t \t \t \t \t \t \t alert('Correct'); \t \t \t \t \t \t \t
\t \t \t \t \t \t \t correct(i);
\t \t \t \t \t \t \t var counter = counter + 1; //Adds one to the counter for correct answers
\t \t \t \t \t \t }
\t \t \t \t \t \t //Actions for wrong answer
\t \t \t \t \t \t else
\t \t \t \t \t \t { \t \t \t \t \t \t
\t \t \t \t \t \t \t alert('Wrong');
\t \t \t \t \t \t \t wrong(i);
\t \t \t \t \t \t }
\t \t \t \t }
//Functions
\t \t \t function correct(i)
\t \t \t {
\t \t \t \t document.write(i + 1, ". Correct" + "<br>");
\t \t \t } \t
\t \t \t function wrong(i)
\t \t \t {
\t \t \t \t document.write(i + 1, ". Wrong, correct answer = ", answer[i], "<br>");
\t \t \t }
//Calculates the results based on the counter
\t \t \t document.write("<br>You got " + counter + " answers out of 5 correct.");
\t \t </script>
\t </p>
</body>
</html>
を追加するために使用するjQueryを使用しています。代わりに、一部のタグのinnerHTMLまたは.html() – mplungjan
コードをクリーンアップしてください。 [を使用してリンクします。 –
また、jQueryスクリプトを入力しないでください。 –