You can declare a number which represents the sentence number, increment it at the button click and display the i
th sentence modulo sentences array length in order to have a loop effect.(您可以声明一个表示句子编号的数字,在单击按钮时将其递增,并显示第i
个句子模句子数组的长度,以产生循环效果。)
Like this:(像这样:)
var sentences = ['first', 'second', 'third', 'fourth']; var senNum = 0; var sen = document.getElementById('sen'); sen.innerText = sentences[senNum++]; // set initial sentence document.getElementById('change').addEventListener('click', function(e) { sen.innerText = sentences[senNum++ % sentences.length]; });
<div id='sen'></div> <button id="change">Change</button>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…