gpt4 book ai didi

javascript - 如何从 div 数组中获取下一个对象?

转载 作者:行者123 更新时间:2023-12-01 02:21:56 25 4
gpt4 key购买 nike

我正在尝试循环浏览包含我所有琐事问题的 Div 列表。我知道如何使用迭代器循环遍历所有这些对象,但我只想知道如何获取一个 Div,运行一些代码,然后在用户单击“提交”后循环到数组中的下一个对象,然后运行相同的代码等.我还隐藏了每个 div,这样他们一次只能看到一个 Div,直到他们完成上一个问题。对 Java 脚本非常陌生,并试图尽快学习,任何提示将不胜感激

 <div class="row">
<div class="col-12 questions">
<form name="quiz" onsubmit="return false">
<div class="q1">

<h1>The 'Mountain' is the nickname for which character?</h1>
<input type="radio" name="q" value="wrong" id="q1a">a. &nbsp; Gerold Clegane
<br>
<input type="radio" name="q" value="wrong" id="q1b">b. &nbsp; Sandor clegane
<br>
<input type="radio" name="q" value="wrong" id="q1c">c. &nbsp; Oberyn Martell
<br>
<input type="radio" name="q" value="correct" id="q1d">d. &nbsp; Gregor Clegane
<br>
<button class="submit" type="submit">Submit</button>
</div>

<div class="q2">
<h2>Who is the youngest child of Lord Tywin Lannister?</h2>
<input type="radio" name="q" value="correct" id="q2a">a. &nbsp; Tyrion Lannister
<br>
<input type="radio" name="q" value="wrong" id="q2b">b. &nbsp; Jaime Lannister
<br>
<input type="radio" name="q" value="wrong" id="q2c">c. &nbsp; Cersei Lannister
<br>
<input type="radio" name="q" value="wrong" id="q2d">d. &nbsp; Jon Snow
<br>
<button class="submit" type="submit">Submit</button>
</div>
<div class="q3">

<h3>Who is the King of the North?</h3>
<input type="radio" name="q" value="wrong" id="q3a">a. &nbsp; Bran Stark
<br>
<input type="radio" name="q" value="correct" id="q3b">b. &nbsp; Jon Snow
<br>
<input type="radio" name="q" value="wrong" id="q3c">c. &nbsp; Tommen Baratheon
<br>
<input type="radio" name="q" value="wrong" id="q3d">d. &nbsp; LittleFinger
<br>
<button class="submit" type="submit">Submit</button>
</div>
<div class="q4">

<h4>Who is the head of house Stark?</h4>
<input type="radio" name="q" value="wrong" id="q4a">a. &nbsp; Tyrion Lannister
<br>
<input type="radio" name="q" value="wrong" id="q4b">b. &nbsp; Jon Snow
<br>
<input type="radio" name="q" value="wrong" id="q4c">c. &nbsp; Bran Stark
<br>
<input type="radio" name="q" value="correct" id="q4d">d. &nbsp; Ned Stark
<br>
<button class="submit" type="submit">Submit</button>
</div>
<div class="q5">

<h5>Which persons are the 'Night's Watch' trying to stop by using a giant wall of ice?</h5>
<input type="radio" name="q" value="correct" id="q5a">a. &nbsp; White Walkers
<br>
<input type="radio" name="q" value="wrong" id="q5b">b. &nbsp; Wildings
<br>
<input type="radio" name="q" value="wrong" id="q5c">c. &nbsp; Mother of Dragons
<br>
<input type="radio" name="q" value="wrong" id="q5d">d. &nbsp; Night walkers
<br>
<button class="submit" type="submit">Submit</button>
</div>
</form>

这是我的 JavaScript

$(document).ready(function () {
console.log("ready!");

var question1 = $('.q1');
var question2 = $('.q2');
var question3 = $('.q3');
var question4 = $('.q4');
var question5 = $('.q5');

var correctAnswers = 0;
var wrongAnswers = 0;
var gameArr = [question1, question2, question3, question4, question5];

function nextGame() {
for (i=0;i<gameArr.length;i++){
//i dont want it to grab every object all at the same time
}



}
nextGame();
console.log(nextGame());

//My startGame button
$('.b1').on("click", function () {
console.log(gameArr[0]);
gameArr[0].show();
});

//Setting up audio for the start screen, make it loop so it never stops running
var audio1 = new Audio("assets/music/startMusic.mp3");

audio1.addEventListener('ended', function () {
this.currentTime = 0;
this.play();
}, false);
audio1.play();

//code to make my quiz responsive
function game() {

var answer = $("input[name='q']:checked").val();
if (answer === "correct") {
alert("Correct!");
correctAnswers = correctAnswers + 1

} else {
wrongAnswers = wrongAnswers + 1
alert("Wrong!");

}
}

//timer
function countdown() {
seconds = 60;
$('#timer').html('<h6>Time Remaining: ' + seconds + '</h6>');

time = setInterval(showCountdown, 1000);
}

function showCountdown() {
seconds--;
$('#timer').html('<h3>Time Remaining: ' + seconds + '</h3>');
if (seconds < 1) {
clearInterval(time);


}
}

//submit your answer
$('.submit').on("click", function () {
//nextGame();
game();

console.log(gameArr);
});

最佳答案

你必须记住最后的位置。

所以你有一个变量,将其命名为lastGame 之类的。这将是与正确答案等处于同一级别的变量。从零开始。

每当您想要导航到下一个门时,请增加此值并执行 gameArr[lastGame].show();

示例:

var correctAnswers = 0;
var wrongAnswers = 0;
var gameArr = [question1, question2, question3, question4, question5];
var lastGame = 0;

function nextGame() {
gameArr[lastGame].show();
lastGame++;
}

超时时您调用nextGame()

关于javascript - 如何从 div 数组中获取下一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49138445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com