Javascript 真与假循环数组
我试图在一个数组上循环,以便在显示正确或错误答案时显示下一个问题。单击“下一步”时,我希望隐藏上一条语句并显示下一条语句。这是我的一段代码Javascript 真与假循环数组,javascript,jquery,Javascript,Jquery,我试图在一个数组上循环,以便在显示正确或错误答案时显示下一个问题。单击“下一步”时,我希望隐藏上一条语句并显示下一条语句。这是我的一段代码 <body> <div class="row"> <div class="large-3 medium-3 small-3 small-centered columns"> <div class="content"></div> <button class="tr
<body>
<div class="row">
<div class="large-3 medium-3 small-3 small-centered columns">
<div class="content"></div>
<button class="true">True</button>
<button class="false">False</button>
</div>
</div>
<script>
$(document).ready(function(){
var questions = ['The Earth is round', 'The sun is yellow', 'Dogs are reptiles'];
var answerTrue = ['You Got It!'];
var answerFalse = ['Dogs are mamals'];
$('button.true').click(function(){
$('.content').text(questions[1]);
});
});
</script>
真的
假的
$(文档).ready(函数(){
var questions=[‘地球是圆的’、‘太阳是黄的’、‘狗是爬行动物’;
var answerTrue=[“你明白了!”;
var answerFalse=[“狗是哺乳动物”];
$('button.true')。单击(函数(){
$('.content')。文本(问题[1]);
});
});
你需要一个计数器
$(document).ready(function()) {
var questions = ['The Earth is round', 'The sun is yellow', 'Dogs are reptiles'];
var answerTrue = ['You Got It!'];
var answerFalse = ['Dogs are mammals'];
var counter = 0;
$('button.true').click(function(){
if(counter < questions.length) {
$('.content').text(questions[counter]);
counter = counter + 1;
}
});
}
$(文档).ready(函数()){
var questions=[‘地球是圆的’、‘太阳是黄的’、‘狗是爬行动物’;
var answerTrue=[“你明白了!”;
var answerFalse=[“狗是哺乳动物”];
var计数器=0;
$('button.true')。单击(函数(){
如果(计数器<问题长度){
$('.content').text(问题[计数器]);
计数器=计数器+1;
}
});
}
您需要一个计数器
$(document).ready(function()) {
var questions = ['The Earth is round', 'The sun is yellow', 'Dogs are reptiles'];
var answerTrue = ['You Got It!'];
var answerFalse = ['Dogs are mammals'];
var counter = 0;
$('button.true').click(function(){
if(counter < questions.length) {
$('.content').text(questions[counter]);
counter = counter + 1;
}
});
}
$(文档).ready(函数()){
var questions=[‘地球是圆的’、‘太阳是黄的’、‘狗是爬行动物’;
var answerTrue=[“你明白了!”;
var answerFalse=[“狗是哺乳动物”];
var计数器=0;
$('button.true')。单击(函数(){
如果(计数器<问题长度){
$('.content').text(问题[计数器]);
计数器=计数器+1;
}
});
}
您需要一个计数器
$(document).ready(function()) {
var questions = ['The Earth is round', 'The sun is yellow', 'Dogs are reptiles'];
var answerTrue = ['You Got It!'];
var answerFalse = ['Dogs are mammals'];
var counter = 0;
$('button.true').click(function(){
if(counter < questions.length) {
$('.content').text(questions[counter]);
counter = counter + 1;
}
});
}
$(文档).ready(函数()){
var questions=[‘地球是圆的’、‘太阳是黄的’、‘狗是爬行动物’;
var answerTrue=[“你明白了!”;
var answerFalse=[“狗是哺乳动物”];
var计数器=0;
$('button.true')。单击(函数(){
如果(计数器<问题长度){
$('.content').text(问题[计数器]);
计数器=计数器+1;
}
});
}
您需要一个计数器
$(document).ready(function()) {
var questions = ['The Earth is round', 'The sun is yellow', 'Dogs are reptiles'];
var answerTrue = ['You Got It!'];
var answerFalse = ['Dogs are mammals'];
var counter = 0;
$('button.true').click(function(){
if(counter < questions.length) {
$('.content').text(questions[counter]);
counter = counter + 1;
}
});
}
$(文档).ready(函数()){
var questions=[‘地球是圆的’、‘太阳是黄的’、‘狗是爬行动物’;
var answerTrue=[“你明白了!”;
var answerFalse=[“狗是哺乳动物”];
var计数器=0;
$('button.true')。单击(函数(){
如果(计数器<问题长度){
$('.content').text(问题[计数器]);
计数器=计数器+1;
}
});
}
使用环形计数器代替计数器,以避免在最后一个问题(而是再次显示第一个问题)后跳出数组。您可以使用创建一个隐藏变量用作计数器,因此不能从click函数外部对其进行修改。为此,创建一个声明计数器变量的函数并返回另一个使用该变量的函数,然后立即调用外部函数(因此创建/返回内部函数)。请参阅下面的代码-声音比实际情况复杂得多
$(function () {
var questions = ['The Earth is round', 'The sun is yellow', 'Dogs are reptiles'];
var answerTrue = ['You Got It!'];
var answerFalse = ['Dogs are mammals'];
$('button.true').click(function () {
var counter = 0;
return function () {
$('.content').text(questions[counter]);
counter = ++counter % questions.length;
}
}());
});
使用环形计数器而不是计数器,以避免在最后一个问题之后跳出数组(而是再次显示第一个问题)。您可以使用创建一个隐藏变量用作计数器,因此不能从click函数外部对其进行修改。为此,创建一个声明计数器变量的函数并返回另一个使用该变量的函数,然后立即调用外部函数(因此创建/返回内部函数)。请参阅下面的代码-声音比实际情况复杂得多
$(function () {
var questions = ['The Earth is round', 'The sun is yellow', 'Dogs are reptiles'];
var answerTrue = ['You Got It!'];
var answerFalse = ['Dogs are mammals'];
$('button.true').click(function () {
var counter = 0;
return function () {
$('.content').text(questions[counter]);
counter = ++counter % questions.length;
}
}());
});
使用环形计数器而不是计数器,以避免在最后一个问题之后跳出数组(而是再次显示第一个问题)。您可以使用创建一个隐藏变量用作计数器,因此不能从click函数外部对其进行修改。为此,创建一个声明计数器变量的函数并返回另一个使用该变量的函数,然后立即调用外部函数(因此创建/返回内部函数)。请参阅下面的代码-声音比实际情况复杂得多
$(function () {
var questions = ['The Earth is round', 'The sun is yellow', 'Dogs are reptiles'];
var answerTrue = ['You Got It!'];
var answerFalse = ['Dogs are mammals'];
$('button.true').click(function () {
var counter = 0;
return function () {
$('.content').text(questions[counter]);
counter = ++counter % questions.length;
}
}());
});
使用环形计数器而不是计数器,以避免在最后一个问题之后跳出数组(而是再次显示第一个问题)。您可以使用创建一个隐藏变量用作计数器,因此不能从click函数外部对其进行修改。为此,创建一个声明计数器变量的函数并返回另一个使用该变量的函数,然后立即调用外部函数(因此创建/返回内部函数)。请参阅下面的代码-声音比实际情况复杂得多
$(function () {
var questions = ['The Earth is round', 'The sun is yellow', 'Dogs are reptiles'];
var answerTrue = ['You Got It!'];
var answerFalse = ['Dogs are mammals'];
$('button.true').click(function () {
var counter = 0;
return function () {
$('.content').text(questions[counter]);
counter = ++counter % questions.length;
}
}());
});
使用嵌套数组
$(document).ready(function () {
var questions = [
["Q1?", "true", "Noooo, beer!"],
["Q2?", "false", "Impossible!"],
["Q3?", "true", "It's more like a thing"],
["Q4?", "false", "Yes, but more like no"]
];
var i = 0;
$('.content').text(questions[i][0]);
$('button').click(function () {
var target = event.target;
if ($(target).attr('class') == questions[i][1]) {
i++;
$('.content').text(questions[i][0]);
}
else alert(questions[i][2]);
});
});
使用嵌套数组
$(document).ready(function () {
var questions = [
["Q1?", "true", "Noooo, beer!"],
["Q2?", "false", "Impossible!"],
["Q3?", "true", "It's more like a thing"],
["Q4?", "false", "Yes, but more like no"]
];
var i = 0;
$('.content').text(questions[i][0]);
$('button').click(function () {
var target = event.target;
if ($(target).attr('class') == questions[i][1]) {
i++;
$('.content').text(questions[i][0]);
}
else alert(questions[i][2]);
});
});
使用嵌套数组
$(document).ready(function () {
var questions = [
["Q1?", "true", "Noooo, beer!"],
["Q2?", "false", "Impossible!"],
["Q3?", "true", "It's more like a thing"],
["Q4?", "false", "Yes, but more like no"]
];
var i = 0;
$('.content').text(questions[i][0]);
$('button').click(function () {
var target = event.target;
if ($(target).attr('class') == questions[i][1]) {
i++;
$('.content').text(questions[i][0]);
}
else alert(questions[i][2]);
});
});
使用嵌套数组
$(document).ready(function () {
var questions = [
["Q1?", "true", "Noooo, beer!"],
["Q2?", "false", "Impossible!"],
["Q3?", "true", "It's more like a thing"],
["Q4?", "false", "Yes, but more like no"]
];
var i = 0;
$('.content').text(questions[i][0]);
$('button').click(function () {
var target = event.target;
if ($(target).attr('class') == questions[i][1]) {
i++;
$('.content').text(questions[i][0]);
}
else alert(questions[i][2]);
});
});
jeff_kile的回答完全正确,你需要一个计数器来跟踪你在一系列问题中的位置 值得一提的是,我做了一些注释,希望能让事情变得更清楚;请注意,这根本不是专家级javascript,我特意让它非常简单,以演示这些概念 你可以找到它 作为参考,代码为:
//setup array of questions with text, correct answer and what to display if the user gives
//and incorrect answer
var questions=[
{text:'The Earth is Round',answer:true,note:'It\'s not flat'},
{text:'The sun is yellow',answer:true,note:'no really it is'},
{text:'Dogs are reptiles',answer:false,note:'What planet are you from?'}
];
//question array index
var index=0;
//helper function to display the content
var setContent=function(text)
{
$(".content").text(text);
}
//helper function to show the current question
var showQuestion=function(index)
{
question=questions[index];
setContent(question.text);
}
//helper function that checks if the user supplied answer is correct
//and if so eother advances the question index or ends the game
//otherwise, displays the question note.
var checkAnswer=function(userAnswer)
{
question=questions[index];
if (question.answer==userAnswer)
{
index++;
if (index>=questions.length)
{
setContent("Thanks for playing");
}
else
{
showQuestion(index);
}
}
else
{
setContent(question.note);
}
}
//wireup clicks to send the answer for the current question to the answer check function
//and kicks off the game with question 0
$(document).ready(function(){
$(".true").click(function(){
checkAnswer(true);
});
$(".false").click(function(){
checkAnswer(false);
});
showQuestion(index);
});
使用此html:
<div class="row">
<div class="large-3 medium-3 small-3 small-centered columns">
<div class="content"></div>
<button class="true">True</button>
<button class="false">False</button>
</div>
</div>
jeff_kile的回答完全正确,你需要一个计数器来跟踪你在一系列问题中的位置 值得一提的是,我做了一些注释,希望能让事情变得更清楚;请注意,这根本不是专家级javascript,我特意让它非常简单,以演示这些概念 你可以找到它 作为参考,代码为:
//setup array of questions with text, correct answer and what to display if the user gives
//and incorrect answer
var questions=[
{text:'The Earth is Round',answer:true,note:'It\'s not flat'},
{text:'The sun is yellow',answer:true,note:'no really it is'},
{text:'Dogs are reptiles',answer:false,note:'What planet are you from?'}
];
//question array index
var index=0;
//helper function to display the content
var setContent=function(text)
{
$(".content").text(text);
}
//helper function to show the current question
var showQuestion=function(index)
{
question=questions[index];
setContent(question.text);
}
//helper function that checks if the user supplied answer is correct
//and if so eother advances the question index or ends the game
//otherwise, displays the question note.
var checkAnswer=function(userAnswer)
{
question=questions[index];
if (question.answer==userAnswer)
{
index++;
if (index>=questions.length)
{
setContent("Thanks for playing");
}
else
{
showQuestion(index);
}
}
else
{
setContent(question.note);
}
}
//wireup clicks to send the answer for the current question to the answer check function
//and kicks off the game with question 0
$(document).ready(function(){
$(".true").click(function(){
checkAnswer(true);
});
$(".false").click(function(){
checkAnswer(false);
});
showQuestion(index);
});
使用此html:
<div class="row">
<div class="large-3 medium-3 small-3 small-centered columns">
<div class="content"></div>
<button class="true">True</button>
<button class="false">False</button>
</div>
</div>
jeff_kile的回答完全正确,你需要一个计数器来跟踪你在一系列问题中的位置 值得一提的是,我做了一些注释,希望能让事情变得更清楚;请注意,这根本不是专家级javascript,我特意让它非常简单,以演示这些概念 你可以找到它 作为参考,代码为:
//setup array of questions with text, correct answer and what to display if the user gives
//and incorrect answer
var questions=[
{text:'The Earth is Round',answer:true,note:'It\'s not flat'},
{text:'The sun is yellow',answer:true,note:'no really it is'},
{text:'Dogs are reptiles',answer:false,note:'What planet are you from?'}
];
//question array index
var index=0;
//helper function to display the content
var setContent=function(text)
{
$(".content").text(text);
}
//helper function to show the current question
var showQuestion=function(index)
{
question=questions[index];
setContent(question.text);
}
//helper function that checks if the user supplied answer is correct
//and if so eother advances the question index or ends the game
//otherwise, displays the question note.
var checkAnswer=function(userAnswer)
{
question=questions[index];
if (question.answer==userAnswer)
{
index++;
if (index>=questions.length)
{
setContent("Thanks for playing");
}
else
{
showQuestion(index);
}
}
else
{
setContent(question.note);
}
}
//wireup clicks to send the answer for the current question to the answer check function
//and kicks off the game with question 0
$(document).ready(function(){
$(".true").click(function(){
checkAnswer(true);
});
$(".false").click(function(){
checkAnswer(false);
});
showQuestion(index);
});
使用此html:<