我正在按下按钮激活功能将颜色更改为随机颜色,但是当它为绿色时,我希望按钮停止更改颜色。
<html>
<head>
<script type="text/javascript">
function roll1() {
rand = Math.ceil(Math.random() * 6);
die = document.getElementById("die1");
if (rand >= 1 && rand <= 3) {
die.innerHTML = "<p>'GREEN'<\/p>";
} else if (rand == 4 || rand == 5) {
die.innerHTML = "<p>'GREEN'<\/p>";
} else if (rand == 6) {
die.innerHTML = "<p>'RED'<\/p>"
}
}
</script>
</head>
<body>
<button onclick="roll1()">Roll Dice</button>
<table>
<tr>
<td style="width:55px; height:55px;">
<p id="die1">'YELLOW'</p></td>
</tr>
</table>
</body>
</html>
我也不希望按钮消失,因为它仍然需要执行此代码中没有的其他操作。
答案 0 :(得分:0)
var stop_flag = false;
function roll1() {
if (stop_flag)
return false;
rand = Math.ceil(Math.random() * 6);
die = document.getElementById("die1");
if (rand >= 1 && rand <= 3) {
stop_flag = true;
die.innerHTML = "<p>'GREEN'<\/p>";
} else if (rand == 4 || rand == 5) {
die.innerHTML = "<p>'GREEN'<\/p>";
} else if (rand == 6) {
die.innerHTML = "<p>'RED'<\/p>"
}
}
答案 1 :(得分:0)
您可以添加属性disabled
,因此该按钮不再可点击。