私はJava、CSS、HTMLを使ってJavaScriptでクイズアプリケーションを開発しています。私は同じ文書(Notepad ++)上のすべてのコードを必要とし、JavaScriptはHTML Idにリンクしていません。 誰かを助けることができますか?<script>をHTMLクイズにリンクするにはどうすればよいですか?
<!DOCTYPE html>
<html lang="en">
<head>
<title>Quiz</title>
</head>
<style>
body {
background-color: #eeeeee;
}
.grid {
width: 600px;
height: 500px;
margin: 0 auto;
background-color: #fff;
padding: 10px 50px 50px 50px;
border-radius: 50px;
border: 2px solid #cbcbcb;
box-shadow: 10px 15px 5px #cbcbcb
}
.grid h1 {
font-family: sans-serif;
background-color: #57636e;
font-size: 60px;
text-align: center;
color: #fff;
padding: 2px 0px border-radius: 50px
}
.grid #question {
font-family: sans-serif;
font-size: 30px;
color: #5a6772
}
.buttons {
margin-top: 30px;
}
#btn0,
#btn1,
#btn2,
#btn3 {
background-color: #778897;
width: 250px;
font-size: 20px;
color: #fff;
border: 1px solid #1d3c6a;
border-radius: 50px;
margin: 10px 40px 10px 0px;
padding: 10px 10px;
}
#progress {
color: #2b2b2b;
font-size: 18px;
}
#btn0:hover,
#btn1:hover,
#btn2:hover,
#btn3:hover {
cursor: pointer;
background-color: #57636e;
}
#btn0:focus,
#btn1:focus,
#btn2:focus,
#btn3:focus {
outline: 0;
}
</style>
<body>
<div class="grid">
<div id="quiz">
<h1>Quiz</h1>
<hr style="margin-bottom: 20px">
<p id="question"></p>
<div class="buttons">
<button id="btn0"><span id="choice0"></span></button>
<button id="btn1"><span id="choice1"></span></button>
<button id="btn2"><span id="choice2"></span></button>
<button id="btn3"><span id="choice3"></span></button>
</div>
<hr style="margin-top: 50px">
<footer>
<p id="progress">Question x of y.</p>
</footer>
</div>
</div>
<script language="javascript">
function Quiz(questions) {
this.score = 0;
this.questions = questions;
this.questionIndex = 0;
}
Quiz.prototype.getQuestionIndex = function() {
return this.questions(this.questionIndex);
}
Quiz.prototype.isIndex = function() {
return this.questions.length === this.questionIndex;
}
Quiz.prototype.guess = function(answer) {
this.questionIndex++;
if (this.getQuestionIndex().correctAnswer(answer)) {
this.score++;
}
}
//---------------------------------------------------------------
//Questions.js
function Question(text, choices, answer) {
this.text = text;
this.choices = choices;
this.answer = answer;
}
Question.prototype.correctAnswer = function(choice) {
return choice === this.answer;
}
//---------------------------------------------------------------
//Apps
function populate() {
if (quiz.isEnded()) {
showScores();
} else {
var element = document.getElementById("question");
element.innerHTML = quiz.getQuestionIndex().text;
var choices = quiz.getQuestionIndex().choices;
for (var i = 0; i < choices.length; i++) {
var element = document.getElementById("choice" + i);
element.innerHTML = choices[i];
guess("btn" + i, choices[i]);
}
}
};
function guess() {
var button = document.getElementById(id);
button.onclick = function() {
quiz.guess(guess);
populate();
}
}
function showScores() {
var gameOverHtml = "<h1>Result</h1>";
gameOverHtml += "<h2 id='score'> Your scores: " + quiz.score + "</h2>";
var element = document.getElementById("quiz");
element.innerHTML = gameOverHtml;
}
var questions = [
new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"),
new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"),
new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"),
new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"),
new Question("Which is not an object?", ["Java", "C#", "C++", "C"], "C"),
];
var quiz = new Quiz(questions);
populate();
</script>
</body>
</html>
多くのおかげ
*スクリプト要素の言語*属性はHTML 4で廃止されましたし、削除されました:だから最後に、あなたのquestionIndexは、アレイは、このように、未定義の質問があり、その後1大きいですずっと前に。 – RobG