【发布时间】:2017-05-21 20:16:49
【问题描述】:
所以我正在使用 Java、CSS 和 HTML 在 JavaScript 中开发一个测验应用程序。我需要同一个文档(Notepad++)上的所有代码,并且 JavaScript 没有链接到 HTML Id。 有人可以帮忙吗?
代码如下:
<!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>
非常感谢
【问题讨论】:
-
脚本元素的 language 属性在 HTML 4 中已弃用,并在很久以前就被删除了。
标签: javascript html css animation