var myArray = ['A','B','C'];document.getElementById("button").onclick = function() { if (myArray.length > 0) { var a = Math.floor(Math.random() * myArray.length); var b = myArray.splice(a, 1); document.getElementById("showRand").innerHTML = b[0]; } else { //restart }}<button id="button">click</button><div id="showRand"></div>Onclick时,它随机清空myArray,如何重新填充数组,然后在myArray为空时重复该过程?感谢您的帮助..
1 回答
心有法竹
TA贡献1866条经验 获得超5个赞
var myArray = ['A','B','C'];
document.getElementById("button").onclick = function() {
if (myArray.length == 0) {
myArray = ['A','B','C'];
}
var a = Math.floor(Math.random() * myArray.length);
var b = myArray.splice(a, 1);
document.getElementById("showRand").innerHTML = b[0];
}
<button id="button">click</button>
<div id="showRand"></div>
添加回答
举报
0/150
提交
取消