简体   繁体   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

The problem is when I click the next button, it goes to the last question.问题是当我单击下一个按钮时,它会转到最后一个问题。 I want when the button is clicked, the loop goes through all the question one by one.我希望在单击按钮时,循环一一解决所有问题。

 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) will fix your error on the code level but it's not what you want. Math.floor((Math.random() * questions.length) + 1)将在代码级别修复您的错误,但这不是您想要的。 If you want to do the prev and next button for all questions, you need to use a variable for the current question's index.如果要对所有问题执行上一个和下一个按钮,则需要为当前问题的索引使用一个变量。

 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>

You need to use a counter to move between the array.您需要使用计数器在阵列之间移动。

next button increments the counter and prev button decrements it.下一个按钮增加计数器,上一个按钮减少它。

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];
});

here is the code pen demo这是代码笔演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我点击按钮时,问题没有进入下一个 - Question is not advancing to the next one when I click the button 单击下一步按钮时问题索引未更新 - Question index not updating when I click next button 如何在不单击下一步按钮的情况下进入下一个问题? - How can I go for next question without click next button? 当我点击下一个按钮时,上一个问题必须用反向 animation 删除,下一个问题必须显示输入效果 animation - When i click on next button, the previous question must be remove with reverse animation & next question must be show with typing effect animation 每次按下下一个问题按钮时,我都想将其更改为数组中写入的内容 - I want to change this to what is written in the array every time a next question button is pushed 当我单击按钮时加载下一行 - Load next row when i click the button <h2>当我单击菜单时未查看 - <h2> is not viewed when i click on the menu 我想在单击按钮时重定向到同一目录的下一页 - - I want to redirect on next page on same directory on button click - 单击下一个/上一个按钮时,如何将活动类更改为LI元素? - How do I change the active class to the LI element when I click next/previous button? 我需要在点击按钮上播放下一首歌 - I need to play next song when click on on-click button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM