所以我正在使用Java,CSS和HTML在JavaScript中开发一个Quiz应用程序。我需要同一个文档(Notepad ++)上的所有代码,并且JavaScript没有链接到HTML Id。
有人可以帮忙吗?


这是代码:



<!DOCTYPE html>
< html lang =“en”>

< head>
&# XA; <标题>测验和LT; /标题>&#的xD;
< /头>&#的xD;
<风格>&#的xD;
身体{
 background-color:#eeeeee;
 }&#的xD;
 &#的xD;
 .grid {
宽度:600px;
身高:500px;
 margin:0 auto;
 background-color:#fff;
填充:10px 50px 50px 50px;
 border-radius:50px;
 border:2px solid #cbcbcb;
 box-shadow:10px 15px 5px#cbcbcb
 }&#的xD;
 &#的xD;
 .grid h1 {
 font-family:sans-serif;
 background-color:#57636e;
 font-size:60px;
 text-align:center;
颜色:#fff;
填充:2px 0px border-radius:50px
 }&#的xD;
 &#的xD;
 .grid #question {
 font-family:sans-serif;
 font-size:30px;
颜色:#5a6772
 }&#的xD;
 &#的xD;
 .buttons {
 margin-top:30px;
 }&#的xD;
 &#的xD;
 #btn0,&#的xD;
 #BTN1,&#的xD;
 #BTN2,&#的xD;
 #btn3 {
 background-color:#778897;
宽度:250px;
 font-size:20px;
颜色:#fff;
 border:1px solid#1d3c6a;
 border-radius:50px;
保证金:10px 40px 10px 0px;
填充:10px 10px;
 }&#的xD;
 &#的xD;
 #progress {
颜色:#2b2b2b;
 font-size:18px;
 }&#的xD;
 &#的xD;
 #btn0:悬停,&#的xD;
 #BTN1:悬停,&#的xD;
 #BTN2:悬停,&#的xD;
 #btn3:悬停{
 cursor:pointer;
 background-color:#57636e;
 }&#的xD;
 &#的xD;
 #btn0:焦点,&#的xD;
 #BTN1:焦点,&#的xD;
 #BTN2:焦点,&#的xD;
 #btn3:focus {
 outline:0;
 }&#的xD;
< /风格>&#的xD;
&#的xD;
<身体GT;&#的xD;
 < div class =“grid”>
 < div id =“quiz”>
 < H1>测验和LT; / H1>&#的xD;
 < 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>&#的xD;
&#的xD;
 < hr style =“margin-top:50px”>
 <页脚>&#的xD;
 < p id =“progress”> y的问题x。< / p>
 < /页脚>&#的xD;
 < / DIV>&#的xD;
 < / DIV>&#的xD;
 < script language =“javascript”>
功能测验(问题){
 this.score = 0;
 this.questions = questions;
 this.questionIndex = 0;
 }&#的xD;
 Quiz.prototype.getQuestionIndex = function(){
 return this.questions(this.questionIndex);
 }&#的xD;
 Quiz.prototype.isIndex = function(){
 return this.questions.length === this.questionIndex;
 }&#的xD;
 Quiz.prototype.guess = function(answer){
 this.questionIndex ++;&#的xD;
 if(this.getQuestionIndex()。correctAnswer(answer)){
 this.score ++;&#的xD;
 }&#的xD;
 }&#的xD;
 // ------------------------------------------------ ---------------&#的xD;
 //Questions.js
功能问题(文字,选择,答案){
 this.text = text;
 this.choices =选择;
 this.answer = answer;
 }&#的xD;
 Question.prototype.correctAnswer = function(choice){
返回选择=== this.answer;
 }&#的xD;
 // ------------------------------------------------ ---------------&#的xD;
 //应用&#的xD;
 function populate(){
 if(quiz.isEnded()){
 showScores();&#的xD;
 } 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];
猜(“btn”+ i,选择[i]);
 }&#的xD;
&#的xD;
 }&#的xD;
 };&#的xD;
&#的xD;
 function guess(){
 var button = document.getElementById(id);
 button.onclick = function(){
 quiz.guess(猜测);&#的xD;
填充();&#的xD;
 }&#的xD;
 }&#的xD;
&#的xD;
 function showScores(){
 var gameOverHtml =“< h1> Result< / h1>”;
 gameOverHtml + =“< h2 id ='score'>你的分数:”+ quiz.score +“< / h2>”;
 var element = document.getElementById(“quiz”);
 element.innerHTML = gameOverHtml;
 }&#的xD;
 var questions = [
新问题(“哪个不是对象?”,[“Java”,“C#”,“C ++”,“C”],“C”),
新问题(“哪个不是对象?”,[“Java”,“C#”,“C ++”,“C”],“C”),
新问题(“哪个不是对象?”,[“Java”,“C#”,“C ++”,“C”],“C”),
新问题(“哪个不是对象?”,[“Java”,“C#”,“C ++”,“C”],“C”),
新问题(“哪个不是对象?”,[“Java”,“C#”,“C ++”,“C”],“C”),
 ];&#的xD;
 var quiz = new Quiz(questions);
填充();&#的xD;
 < /脚本>&#的xD;
< / BODY>&#的xD;
&#的xD;
< / HTML> 代码>
&#的xD ;
 非常感谢

答案 0 :(得分:3)
此应用程序存在大量问题,从缺少参数到缺少功能:
在测验中似乎没有名为isEnded
的功能。
我认为这是在所有问题都被提出时的意思:
Quiz.prototype.isEnded = function() {
return this.questionIndex === this.questions.length;
}
this.questions(this.questionIndex)
。你传递了一系列问题。所以this.questions实际上是一个数组。我怀疑你想要:
Quiz.prototype.getQuestionIndex = function() {
return this.questions[this.questionIndex];
}
你真的不需要内部跨度,实际上可以更容易地得到答案:
<button id="btn0"></button>
<button id="btn1"></button>
<button id="btn2"></button>
<button id="btn3"></button>
将Populate的渲染选项部分更改为:
for (var i = 0; i < choices.length; i++) {
var element = document.getElementById("btn" + i);
element.innerHTML = choices[i];
guess("btn" + i, choices[i]);
}
猜测函数没有定义id
变量,参数
function guess(id) {
var button = document.getElementById(id);
button.onclick = function(event) {
quiz.guess(this.innerHTML);
populate();
}
}
在检查猜测时,您在检查答案之前正在递增。所以最后,你的questionIndex是一个大于数组的问题因此,未定义:
Quiz.prototype.guess = function(answer) {
if (this.getQuestionIndex().correctAnswer(answer)) {
this.score++;
}
this.questionIndex++;
}
<!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="text/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) {
if (this.getQuestionIndex().correctAnswer(answer)) {
this.score++;
}
this.questionIndex++;
}
Quiz.prototype.isEnded = function() {
return this.questionIndex === this.questions.length;
}
//---------------------------------------------------------------
//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("btn" + i);
element.innerHTML = choices[i];
guess("btn" + i, choices[i]);
}
}
};
function guess(id) {
var button = document.getElementById(id);
button.onclick = function(event) {
quiz.guess(this.innerHTML);
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>