我想在每個頁面的末尾創(chuàng)建一個小測驗。我有一個用JavaScript編寫的測驗,但我希望不同的問題出現(xiàn)在不同的頁面中。簡而言之,如果我在其他頁面上,我希望const myQuestions更改。我怎樣才能做到這一點?我已經(jīng)嘗試過if語句,但無法實現(xiàn)所需的功能。任何幫助將不勝感激!這是測驗的JS代碼(function() { const myQuestions = [ { question: "question 1?", answers: { a: "one", b: "two", c: "three" }, correctAnswer: "c" }, { question: "question 2?", answers: { a: "one", b: "two", c: "three" }, correctAnswer: "c" }, { question: "question 3?", answers: { a: "one", b: "two", c: "three" d: "four" }, correctAnswer: "d" } ]; function buildQuiz() { // we'll need a place to store the HTML output const output = []; // for each question... myQuestions.forEach((currentQuestion, questionNumber) => { // we'll want to store the list of answer choices const answers = []; // and for each available answer... for (letter in currentQuestion.answers) { // ...add an HTML radio button answers.push( `<label> <input type="radio" name="question${questionNumber}" value="${letter}"> ${letter} : ${currentQuestion.answers[letter]} </label>` ); } // add this question and its answers to the output output.push( `<div class="slide"> <div class="question"> ${currentQuestion.question} </div> <div class="answers"> ${answers.join("")} </div> </div>` ); }); // finally combine our output list into one string of HTML and put it on the page quizContainer.innerHTML = output.join(""); } function showResults() { // gather answer containers from our quiz const answerContainers = quizContainer.querySelectorAll(".answers"); // keep track of user's answers let numCorrect = 0;
如何創(chuàng)建在不同頁面上有不同問題的測驗?
湖上湖
2021-05-02 12:32:19