【发布时间】:2021-03-23 13:54:47
【问题描述】:
我正在尝试使用 Javascript 创建一个问答游戏。我遇到的问题是问题和答案的 Js 对象没有显示在我的 HTML 页面中。我的主要问题在于 showQuestions 函数。任何帮助将不胜感激。非常感谢。
请在下面找到我的代码。
const startButton = document.getElementById("start");
const timerEl = document.getElementById("timer");
const questionEl = document.getElementById("question");
const ul = document.getElementById("answers");
const resultEl = document.getElementById("result");
let currentQuestionIndex = 0;
let seconds = 60;
let interval;
const questions = [{
question: "How many megabytes are there in 1 Gigabytes?",
answers: [{
answer: "1000",
isCorrect: false
},
{
answer: "100",
isCorrect: false
},
{
answer: "10,000",
isCorrect: true
}
],
},
{
question: "what is C#?",
answers: [{
answer: "A procedural language",
isCorrect: false
},
{
answer: "An object-oriented language",
isCorrect: true
},
{
answer: "a functional programming language",
isCorrect: false
}
],
},
{
question: "What do you call a group of memory location containing the same data type?",
answers: [{
answer: "A loop",
isCorrect: false
},
{
answer: "Data structure",
isCorrect: false
},
{
answer: "Array",
isCorrect: true
}
],
},
{
question: "In C++ which keyword must you use to display and anser on the screen?",
answers: [{
answer: "printf",
isCorrect: true
},
{
answer: "printout",
isCorrect: false
},
{
answer: "msgout",
isCorrect: false
}
],
},
{
question: "Which keyword do you use in C# to declare variables?",
answers: [{
answer: "let",
isCorrect: false
},
{
answer: "Dim",
isCorrect: true
},
{
answer: "var",
isCorrect: false
}
],
},
];
function startTimer() {
timerEl.textContent = seconds;
interval = setInterval(function() {
seconds--;
timerEl.textContent = seconds;
if (seconds === 0) {
clearInterval(interval);
}
//renderTime is called here once every second
}, 1000);
}
function onButtonClick() {
const value = this.getAttribute("value");
if (value === "true") {
resultEl.textContent = "correct";
} else {
resultEl.textContent = "incorrect";
}
currentQuestionIndex++;
if (currentQuestionIndex === questions.length) {
questionEl.textContent = "FINISH";
} else {
showQuestions(questions[currentQuestionIndex]);
}
}
function showQuestions(question) {
questionEl.textContent = question.question;
//Loop through each questions and answers
question.answers.forEach((currentAnswer) => {
const li = document.createElement("li");
const button = document.createElement("button");
button.setAttribute("value", currentAnswer.isCorrect);
button.textContent = currentAnswer.answer;
button.addEventListener("click", onButtonClick);
li.appendChild(button);
ul.appendChild(li);
})
}
function startGameQuiz() {
startTimer();
showQuestions(questions[currentQuestionIndex]);
}
startButton.addEventListener("click", startGameQuiz);
<html>
<header class="header">
<strong>
<a>010101</a>
</strong>
<span id="timer" class="quizzTimer">Time: 0</span>
</header>
<body>
<h1>Computer Science Quiz</h1>
<br>
<h5>
<p>Welcome to the Computer Science quiz session.</p>
</h5>
<!--Display questions and answers-->
<div id="quiz"></div>
<button id="start">Start quiz</button>
<div id="result"></div>
</body>
<script src="script.js"></script>
</html>
【问题讨论】:
-
1024 MB (1 GB)。
-
@Vbudo 可以。 Kilo、Mega 和 Giga 通常是 SI,即 1000。Kibi、Mebi 和 Gibi 可以为二进制 1024。这有点乱。
-
执行代码时您的 html 可能尚未准备好,因此您的引用为空。将您的初始化代码添加到 onload 事件中。
-
NicSlash 感谢您的意见。我将在 onload 事件中添加一个 init。
标签: javascript html object display