阵列连接按钮?

时间:2018-08-22 20:34:06

标签: javascript html arrays

如何制作连接到阵列的按钮?我希望能够采用以下代码并将其作为数组连接到按钮:

const quotesArray = [
  "Here is quote 1",
  "Here is quote 2",
  "Here is quote 3",
 ];

 // Get DOM elements that we will listen to and change.
 const currentQuote = document.getElementById("current-quote");
 const getNextQuote = document.getElementById('get-next-quote');

 // Set initial quote to display
 currentQuote.innerHTML = quotesArray[0];


 // Set a variable to keep track the next quote to display.
 let i = 1;

 // Listen for a click and set HTML to the next quote in the array.
 getNextQuote.addEventListener('click', () => {
 const nextQuote = quotesArray[i];
 currentQuote.innerHTML = nextQuote;

 // Update i. Set i to 0 if we've reached the end of the array.
 if (i === quotesArray.length - 1) {
   i = 0;
 } else {
  i += 1;
 }
  });
<div>
  <div id="get-next-quote">Get Next Quote</div>
</div>

<div id="current-quote"></div>
    
    
        

我不确定我需要将哪个部分附加到阵列上,并且我想对它做更多的事情,但是我只想弄清楚如何处理阵列并能够直接从按钮访问它。谁能向我解释一下?

0 个答案:

没有答案