gpt4 book ai didi

Javascript "if"语句不起作用

转载 作者:行者123 更新时间:2023-12-03 23:09:56 25 4
gpt4 key购买 nike

我刚接触 Javascript 编程。几个小时以来,我一直在尝试修复这个“if”语句。我看不出有什么问题。请看一下,告诉我哪里出了问题!

    <!DOCTYPE HTML>
<HTML>
<body>
<head>
<style>
button {
font-size:14px
}
button.hide {
display:none
}
</style>
<title>Candy Box REMAKE</title>
<script type="text/javascript">
var candies = 0;
function addCandies() {
candies ++;
document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies.";
};
function add10Candies() {
candies += 10;
document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies.";
};
function throwCandies() {
candies -= 10;
document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies.";
};
if (candies >= 30) {
document.getElementById('add10Candies').style.display = "block";
};
</script>
<p id="numberOfCandies">You got 0 candy</p>
<button onclick="addCandies()">Get a candy.</button>
<button onclick="throwCandies()">Throw 10 candies away.
<button class="hide" id="add10Candies" onclick="add10Candies">Get 10 candies.</button>
</body>
</HTML>

所以这部分:

if (candies >= 30) {
document.getElementById('add10Candies').style.display = "block";
};

当我得到足够的糖果时不起作用(显示隐藏按钮)。

最佳答案

将您的if 语句 放入每个函数中。目前它只在页面加载时运行一次。

关于Javascript "if"语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41260950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com