Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 遍历循环并从数组返回字符串_Javascript_Arrays - Fatal编程技术网

Javascript 遍历循环并从数组返回字符串

Javascript 遍历循环并从数组返回字符串,javascript,arrays,Javascript,Arrays,在每次按下按钮后,是否可以遍历该数组并从数组中逐个返回HTML引号 这是我无法理解的代码: var quotes = ['"Time you enjoy wasting is not wasted time."', '"You can have it all. Just not all at once."', '"They say I am old-fashioned, and live in the past, but sometimes I think progress progresse

在每次按下按钮后,是否可以遍历该数组并从数组中逐个返回HTML引号

这是我无法理解的代码:

var quotes = ['"Time you enjoy wasting is not wasted time."',
'"You can have it all. Just not all at once."', 
'"They say I am old-fashioned, and live in the past, but sometimes I think
progress progresses too fast!"'];

document.getElementById("btn1").addEventListener("click", newQuote);
function newQuote(){
  for (var i = 0; i < quotes.length; i++) {
   document.getElementById("quote").innerHTML = quotes[i];  
  }
}
var quotes=['“你喜欢浪费的时间不是浪费的时间。”,
“你可以拥有一切,只是不能一次拥有一切。”,
“他们说我很守旧,生活在过去,但有时我会想
进步太快了!“];
document.getElementById(“btn1”)。addEventListener(“单击”,新报价);
函数newQuote(){
for(var i=0;i
使用计数器跟踪下一个报价的索引,并在到达最后一个报价后将其环绕:

var nextQuote=0;
变量引号=[
“你喜欢浪费的时间不是浪费的时间。”,
“你可以拥有一切,只是不能一次拥有一切。”,
“他们说我很守旧,生活在过去,但有时我觉得进步太快了!”
];
函数newQuote(){
document.getElementById(“quote”).innerHTML=quotes[nextQuote];
nextQuote=(nextQuote+1)%quotes.length;
}


下一句话
谢谢你的回答!谢谢你的回答!
var quotes = ['"Time you enjoy wasting is not wasted time."',
'"You can have it all. Just not all at once."', 
'"They say I am old-fashioned, and live in the past, but sometimes I think
progress progresses too fast!"'];

var quoteNumber = 0;
document.getElementById("btn1").addEventListener("click", newQuote);
function newQuote(){
  document.getElementById("quote").innerHTML = quotes[quoteNumber];
  quoteNumber++;
  if(quoteNumber == quotes.length) quoteNumber = 0;  
}