我创建了一个数组,当您按下随机单词按钮时,会选择一个随机单词,但是为了继续浏览列表,我一直坚持创建下一个和上一个单词。
<Script>
window.onload = myFunction;
var WordList = ["a", "b", "c", "d"];
var Word
function myFunction() {
var Word = WordList[Math.floor(Math.random()*WordList.length)];
document.getElementById("Word").innerHTML = Word;
}
function NextWord() {
var a = WordList.indexOf("Word");
document.getElementById("Word").innerHTML = a;
</script>
您的nextWord()函数没有右括号,并且您正在寻找字符串“ Word”的索引,而您想查找变量Word的index,就像这样
function NextWord() {
var a = WordList.indexOf(Word) ;
/// check if last entry
if((a + 1) == WordList.length) {
/// reset
a = 0;
}else {
/// increment
a += 1;
}
/// set innerhtml
document.getElementById("Word").innerHTML = WordList[a];
}
您可以对PreviousWord()执行相同的操作
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句