简体   繁体   English

Javascript“if”语句不起作用

[英]Javascript “if” statement does not work

I'm new to programming with Javascript. 我是Javascript编程的新手。 I've been trying fixing this "if" statement for hour. 我一直试图修复这个“if”声明一小时。 I don't see any proplem with it. 我没有看到任何问题。 Please have a look and tell me what's wrong! 请看看,告诉我有什么问题!

  <!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> 

So this part: 这部分:

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

doesn't work (show the hidden button) when i get enough candies. 当我得到足够的糖果时,它不起作用(显示隐藏的按钮)。

put your if statement in each function. if statement放在每个函数中。 At the moment it only runs once, when the page loads. 当页面加载时,它只运行一次。

The issue is that this if check runs only once when you load the page. 问题是, if检查在加载页面时仅运行一次。 A possible solution would be to create a checkCandies() function that is called within all the other functions: 一种可能的解决方案是创建一个在所有其他函数中调用的checkCandies()函数:

var candies = 0;
function addCandies() {
    candies ++;
    document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies.";
    checkCandies();
};
function add10Candies() {
    candies += 10;
    document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies.";
    checkCandies();
};
function throwCandies() {
    candies -= 10;
    document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies.";
    checkCandies();
};

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

Put your code in a function: 将您的代码放在一个函数中:

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

Call it when you update the quantity: 更新数量时调用它:

function addCandies() {
    candies ++;
    document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies.";
    checkCandiesQuantity();
};

function add10Candies() {
    candies += 10;
    document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies.";
    checkCandiesQuantity();
};

function throwCandies() {
    candies -= 10;
    document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies.";
    checkCandiesQuantity();
};

I've add checkIs30Candies() function. 我添加了checkIs30Candies()函数。 Also have changed style to style.display = "inline-block" (in you case it have to be inline-block, I suppose) 也改变了样式到style.display =“inline-block”(在你的情况下,它必须是内联块,我想)

And you had a typo 你有一个错字

 <button class="hide" id="add10Candies" onclick="add10Candies">Get 10 candies.</button> 

It should be onclick="add10Candies()", with braces to invoke a function 它应该是onclick =“add10Candies()”,带有大括号来调用一个函数

 <!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 checkIs30Candies() { if (candies >= 30) { document.getElementById('add10Candies').style.display = "inline-block"; } else { document.getElementById('add10Candies').style.display = "none"; } } function addCandies() { candies ++; document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies."; checkIs30Candies(); }; function add10Candies() { candies += 10; document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies."; }; function throwCandies() { candies -= 10; document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies."; checkIs30Candies(); }; </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> 

You should put the if statement in other functions that change candies amount. 您应该将if语句放在其他更改糖果数量的函数中。

function addCandies() {
    candies ++;
    document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies.";

    checkCandies();
}

function add10Candies() {
    candies += 10;
    document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies.";

    checkCandies();
}

function throwCandies() {
    candies -= 10;
    document.getElementById('numberOfCandies').innerHTML = "You got " + candies + " candies.";

    checkCandies();
}

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

You need to add this code 您需要添加此代码

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

in each of the functions you have defined. 在您定义的每个功能中。

What is happening right now : 现在发生了什么:

Step 1 : 第1步 :

var candies = 0;

Step 2 : 第2步 :

<declarations of functions>

Step 3 : 第3步:

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

So after initialization and function declarations, the control directly passes to your if condition and since candies = 0 nothing happens. 因此,在初始化和函数声明之后,控件直接传递给你的if条件,因为candies = 0没有任何反应。

When you call any of these functions, the function executes but since the if condition is outside the function, it is not executed. 当您调用这些函数中的任何一个时,函数会执行,但由于if条件在函数之外,因此不会执行。

Perhaps one simpler way to write this is (there are also increasing levels of 'simpler' as well). 或许写一个更简单的方法是(也有更多'更简单'的水平)。 Reuse the common bits of code when you can. 尽可能重用常见的代码。

PS: I fixed what I assumed to be a grammatical error in the message: PS:我修复了我认为是消息中的语法错误:

<script type="text/javascript">
var candies = 0;
function updateCandies() {
    var e = document.getElementById('numberOfCandies');
    e.innerHTML = "You [edit:] have " + candies + " candies.";
    e.style.display = candies>=30 ? "block" : "none";
}

function addCandies() {
    candies ++;
    updateCandies();
};
function add10Candies() {
    candies += 10;
    updateCandies();
};
function throwCandies() {
    candies -= 10;
    updateCandies();
};
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM