<請益>JS/html 若隨機顯示的圖片要多張,要改哪裡?(目前是只有顯示一張)
主要是利用下面語法,要顯示幾張就複製貼上幾次,有沒有哪個語法,可直接填寫數字,可以取代此語法?
index = Math.floor(Math.random() * tips.length);
document.write(tips[index]);
for循环 一下
for(var i=0;i<3;i++){
let index=Math.floor() //上面的代码 。我就不写全了
document.writte(tips[index])
}
1、Math.random() 返回的是 0.0 ~ 1.0(不包含) 之间的一个伪随机数。
2、你定义的 tips[0] ..tips[3] 是整数,需要 Math.random() * 10
3、可以进行取余操作,* 10 % 4,返回值为小于0-3数
例:
<script>
document.write(Math.floor(Math.random() * 10 % 4))
</script>
推推~