我的onclick
函数正常运行,但问题是jQuery因为某些原因不能为我工作,所以我把它拿出去了,但我不知道如何禁用它onclick
没有使用jQuery:如何禁用按钮onclick
并让它在正确的情况下保持禁用状态(如果不正确则重置?我还需要制作它使numOne
和numTwo
重新计算另一个随机数,如果它等于禁用按钮的ID,那么我猜测我已经改变了do while
循环我必须确保numTwo
永远不等于numOne
,但我不确定如何做到这一点。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Lemon Squares - Grade 1 Edition</title>
</head>
<body id="bodyId">
<script type="text/javascript">
var button = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
var numbers = new Array();
for (var i=0; i<16; i++)
{
var randomNumber = Math.floor(Math.random()*9);
numbers[i] = document.createElement("button");
numbers[i].innerHTML = button[randomNumber];
numbers[i].setAttribute("type", "button");
numbers[i].setAttribute("id", button[i]);
numbers[i].setAttribute("onclick", "onClick('" + button[randomNumber] + "')");
numbers[i].setAttribute("value", button[randomNumber]);
document.getElementById("bodyId").appendChild(numbers[i]);
if (i == 3 || i == 7 || i == 11)
{
document.write("<br>");
}
}
var numOne = (Math.ceil(Math.random()*15)).toString();
var numTwo = (Math.ceil(Math.random()*15)).toString();
do
{
numTwo = Math.ceil(Math.random()*15).toString();
}
while (numTwo == numOne);
var numberOne = document.getElementById(numOne).value;
var numberTwo = document.getElementById(numTwo).value;
var answer = parseInt(numberOne) + parseInt(numberTwo);
var total = 0
function onClick(x)
{
x = parseInt(x);
total += x;
if (total > answer)
{
alert("Sorry, try again.")
total = 0
}
else if(total == answer)
{
alert("Congratulations, you did it!");
total = 0
}
}
document.write("<br> Pick two numbers that add up to: " + answer);
</script>
</body>
</html>
答案 0 :(得分:0)
我认为你这是错误的做法。
游戏的逻辑是(如果我理解正确的话):
所以对你的问题:
单击每个按钮执行以下步骤:
我希望这会有所帮助。