每个“骰子”都应该显示一张图片中的随机array
图片。如何使用 更改每个标签的src
属性值?<img>
for loop
我试图使用for循环,但它不起作用......
感谢您的提示和帮助。
const dice1 = document.getElementById('dice1');
const dice2 = document.getElementById('dice2');
const dice3 = document.getElementById('dice3');
const dice4 = document.getElementById('dice4');
const dice5 = document.getElementById('dice5');
const dice6 = document.getElementById('dice6');
const button = document.getElementById('button');
const imgs = [dice1, dice2, dice3, dice4, dice5, dice6];
let pics = ['https://i.postimg.cc/MfVDpSmQ/Eagle.jpg',
'https://i.postimg.cc/QKGQPzZx/Man.jpg',
'https://i.postimg.cc/1g7MWMzf/Dog.jpg',
'https://i.postimg.cc/xc9HzM07/Telephone.jpg',
'https://i.postimg.cc/4mwcbyy3/Robot.jpg',
'https://i.postimg.cc/ctRRNcd7/Moon.jpg',
'https://i.postimg.cc/xJW5QCMQ/Dinosaur.jpg',
'https://i.postimg.cc/hhdMrLRt/Fish.jpg',
'https://i.postimg.cc/Ty1JWmcG/Flowers.jpg'];
const display = (arr) => {
for (let i = 0; i > arr.length; i++){
return i.src = pics[Math.floor(Math.random() * pics.length)]
}
}
button.onclick = display(imgs)