【问题标题】:Loop is Getting Stuck on First Interval循环卡在第一个间隔
【发布时间】:2017-09-22 21:34:44
【问题描述】:

我意识到你们中的一些人会在一秒钟内发现这一点,但由于某种原因,当使用 onclick 命令点击按钮时,我无法让循​​环比初始循环循环更多。

//creat array with my questions
var qArray = [
  'What is the answer to the sum 1 + 1?',
  'If I have two eggs and I drop one how many do I have left?',
  'What are the three primary     colors?'
];

//create variables
var counter = 0;
var theQuestions = document.getElementById('theQuestions');

//loop through array
function questFunc() {
    for (var i = 0; i < qArray.length; i++) {
        theQuestions.innerHTML = qArray[i];
        counter++;
    }
};

任何帮助将不胜感激。我环顾四周但找不到它,我确定这是因为它是如此明显的修复/愚蠢错误:D

谢谢你..

【问题讨论】:

  • 我认为你循环中的变量count 是错误的。也许你的意思是counter
  • 计数器似乎有些无关紧要,但是每次迭代都会覆盖innerHTML,每次调用函数时,它并没有真正改变?
  • 对不起忽略计数器,你写的,它有一个错字,但功能仍然不起作用。当我单击 html 中的按钮时,我想循环浏览问题,每次单击它都会显示另一个问题。直到打到最后。目前它跳到最后一个。 HTML 按钮-->

标签: javascript arrays function for-loop


【解决方案1】:

如果您想在每次单击按钮时显示一个新问题。您可以尝试以下方法:

//creat array with my questions
var qArray = ['What is the answer to the sum 1 + 1?', 'If I have two eggs and I drop one how many do I have left?', 'What are the three primary     colors?'];

//create variables
var counter = 0;
var theQuestions = document.getElementById('theQuestions');

//loop through array
function questFunc() {
    if (counter < qArray.length) {
        theQuestions.innerHTML = qArray[counter];
        counter++;
    }
};

【讨论】:

  • 计数器用于“问题 1/2/3”部分。另外我对使用'i'很感兴趣,因为我认为在循环中使用'i'是你在数组上使用循环的方式..
  • 你说我想循环浏览问题,每次点击都会显示另一个问题所以上面的代码应该适合你还是我误解了你?
  • 不,你明白。我只想知道如何使用变量来做到这一点。对不起,我试图理解它而不仅仅是修复它。
  • 您可以将变量counter 替换为您喜欢的任何变量。重要的是变量在questFunc() 之外声明,因为它的值必须保留。
  • 这个想法是questFunc 将在您单击按钮时运行。所以您不想在每次单击按钮时循环所有问题。
【解决方案2】:

for 循环按预期工作,第一个问题在这一行:theQuestions.innerHTML = qArray[i];。每次更改innerHTML时,它并没有再添加一项,它只是用新的值覆盖了以前的值,所以你可以尝试使用append()来代替。第二个问题是在您的情况下使用 for 循环,这样您只需单击一个按钮即可遍历整个数组。您已经有 counter 变量来存储将在单击下一个按钮后使用的数组索引。可能不是很好的解释,但请随时提出更多问题。

还有一个sn-p:

//creat array with my questions
var qArray = ['What is the answer to the sum 1 + 1?', 'If I have two eggs and I drop one how many do I have left?', 'What are the three primary     colors?'];

//create variables
var counter = 0;
var theQuestions = document.getElementById('theQuestions');

//loop through array
function questFunc() {
    if (counter < qArray.length) {
      theQuestions.append(qArray[counter]);
      counter ++;
    }
};
<button onclick="questFunc()">Click</button>
<div id="theQuestions"></div>

下面是它与innerHTML 的工作方式:

//creat array with my questions
var qArray = ['What is the answer to the sum 1 + 1?', 'If I have two eggs and I drop one how many do I have left?', 'What are the three primary     colors?'];

//create variables
var counter = 0;
var theQuestions = document.getElementById('theQuestions');

//loop through array
function questFunc() {
    if (counter < qArray.length) {
      theQuestions.innerHTML = qArray[counter];
      counter ++;
    }
};
<button onclick="questFunc()">Click</button>
<div id="theQuestions"></div>

【讨论】:

  • 嗨,不幸的是,这似乎不起作用。我也想知道你为什么最后叫它?那是不是和sn-p一样,因为你没有我有的html(点击IE按钮)?
  • @11-,嗨。 “你没有我拥有的 html” - 没错。 “不幸的是,这似乎不起作用” - 所有三个数组项都已附加,这不是您试图实现的目标吗?如果是这样,请说,预期的结果是什么。
  • 对不起,我没有像我想的那么清楚。我希望每个循环都出现一个问题(每次单击我的 html 按钮时)。
  • @11-,简单,我已经更新了答案。这是预期的结果吗?
  • 是的,谢谢,这就是我想要实现的目标,但我不明白的是为什么我的循环不起作用。是什么让它在整个数组中循环?
猜你喜欢
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 2014-07-21
  • 2010-12-17
  • 1970-01-01
  • 2019-02-06
  • 2022-01-23
相关资源
最近更新 更多