簡體   English   中英

我有 5 個問題。 當我單擊下一個按鈕時,我希望下一個問題顯示在 h2 元素中

[英]I have an array of 5 questions. When I click the next button, I want the next question to be shown in the h2 element

問題是當我單擊下一個按鈕時,它會轉到最后一個問題。 我希望在單擊按鈕時,循環一一解決所有問題。

 var showquestion = document.querySelector('.content h2'); var nextbtn = document.getElementById('next'); var prevbtn = document.getElementById('prev'); //set questions in array var questions = [ "hell I am the first question", "hell I am the second question", "hell I am the thirs question", "hell I am the fourth question" ] //next question function nextbtn.addEventListener('click', nextquestion); function nextquestion() { var random = Math.floor() for (var i = 0; i < questions.length; i++) { showquestion.textContent = questions[i]; } }
 <div class="container"> <div class="content"> <h2>Hello the question will go here</h2> <input type="button" name="b1" id="next" value="Next"> <input type="button" name="b2" id="prev" value="Previous"> </div> </div>

Math.floor((Math.random() * questions.length) + 1)將在代碼級別修復您的錯誤,但這不是您想要的。 如果要對所有問題執行上一個和下一個按鈕,則需要為當前問題的索引使用一個變量。

 var showquestion =document.querySelector('.content h2'); var nextbtn = document.getElementById('next'); var prevbtn = document.getElementById('prev'); var count=-1; var first = true; //set questions in array var questions = [ "hell I am the first question", "hell I am the second question", "hell I am the thirs question", "hell I am the fourth question" ] //next question function nextbtn.addEventListener('click',nextquestion); prevbtn.addEventListener('click',prevquestion); function nextquestion(){ first=false; var random = Math.floor() count++; count=count%4; showquestion.textContent = questions[count]; } function prevquestion(){ var random = Math.floor() if(first){ first=false; count++; } count--; if(count < 0)count=count+4; showquestion.textContent = questions[count]; }
 <div class="container"> <div class="content"> <h2>Hello the question will go here</h2> <input type="button" name="b1" id="next" value="Next"> <input type="button" name="b2" id="prev" value="Previous"> </div> </div>

您需要使用計數器在陣列之間移動。

下一個按鈕增加計數器,上一個按鈕減少它。

var showquestion = document.querySelector('.content h2');
var nextbtn = document.getElementById('next');
var prevbtn = document.getElementById('prev');

//set questions in array

var questions = [
  'hell I am the first question',
  'hell I am the second question',
  'hell I am the thirs question',
  'hell I am the fourth question',
];

let questionCount = 0;
//initial question is set to first element in array
showquestion.textContent = questions[questionCount];

nextbtn.addEventListener('click', () => {
  if (questionCount < questions.length - 1) {
    questionCount++;
  }
  showquestion.textContent = questions[questionCount];
});

prevbtn.addEventListener('click', () => {
  if (questionCount > 0) {
    questionCount--;
  }
  showquestion.textContent = questions[questionCount];
});

這是代碼筆演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM