我用Javascript生成了一个简单的脚本,它生成一个介于0-5 + 1(骰子)之间的随机数。我的问题是,当我按下按钮时,它没有做任何事情,迫使我多次垃圾按钮在骰子出现之前。为什么我的Button有脚本有时无法工作?
我到处寻找,但无法找到任何问题可能是什么。 这里是我的代码:
<html>
<head>
</head>
<body>
<button type="button" onclick="spin()">Click here to roll the Dice!</button>
<img src="" id="demo">
<script>
function spin() {
if (Dice() == 1) {
document.getElementById("demo").src="http://i.imgur.com/QRTs9Ax.png";
}
else if (Dice() == 2) {
document.getElementById("demo").src="http://i.imgur.com/OMz1o8U.png";
}
else if (Dice() == 3) {
document.getElementById("demo").src="http://i.imgur.com/J4Xx2yO.png";
}
else if (Dice() == 4) {
document.getElementById("demo").src="http://i.imgur.com/CJb2ojk.png";
}
else if (Dice() == 5) {
document.getElementById("demo").src="http://i.imgur.com/8W6UL5O.png";
}
else if (Dice() == 6) {
document.getElementById("demo").src="http://i.imgur.com/NGxBete.png";
}
}
</script>
<script>
function Dice() {
return Math.floor(Math.random() * 6) + 1;
}
</script>
</body>
</html>
我不认为这是可以返回7.我肯定能1之间返回值6,请参阅:http://stackoverflow.com/a/4960020/1333836 –