var letters = ["a", "b", "c", "d", "e", "f", "1", "4", "9"]
var letter = letters[Math.floor(Math.random()*letters.length)]
random.addEventListener("click", function(){
quoteDisplay.textContent = letter
})
我在这里做错了什么?无论我在我的字母数组中放什么,当我点击“随机”按钮时,它每次只返回数组的最后一个索引。我希望它从数组中返回一个随机索引,我似乎无法弄清楚为什么它不给我一个随机索引而只是最后一个索引。我究竟做错了什么?
答案 0 :(得分:3)
看起来你需要把你的信件分配给#39;事件监听器内的函数。
const audioSources = ["blue.mp3", "cigar.mp3", "immigrant.mp3", "monkey.mp3", "money.mp3"];
const player = document.getElementById("player");
function playAudio() {
let audioSource = audioSources[Math.floor(Math.random() * audioSources.length)];
player.src = audioSource;
};
player.addEventListener('ended', playAudio);
playAudio(); // start audio playing immediately
这样,每次单击该按钮,都会再次运行var letters = ["a", "b", "c", "d", "e", "f", "1", "4", "9"]
random.addEventListener("click", function(){
var letter = letters[Math.floor(Math.random()*letters.length)]
quoteDisplay.textContent = letter
})
功能。