JavaScript更新全局变量

时间:2014-11-03 16:06:23

标签: javascript html variables global updating

我目前不是Javascript中最有经验的人,而且我一点一点地学习。无论如何......如何更有效地更新平衡变量?

目前我相信我做错了。此外,我的按钮不适用于点击事件。

一切都会是一个巨大的帮助!谢谢。

// Set global variables
var name;
var balance;
var weed;

// Ask the user his name for his character
name = window.prompt("What is your name?", "Cap'n Grow");
var finalName = document.getElementById('name');
finalName.textContent = name;


// Set the balance to default
balance = 100;
var FinalBalance = document.getElementById('balance');
FinalBalance.textContent = balance;

// Set the balance of weed to default 
weed = 10;
var FinalWeed = document.getElementById('gear');
FinalWeed.textContent = weed;

// Sell function
function sellGear() {
    var check = window.prompt("Are you sure you want to sell 5 bags?", "Yes");
    if (check === 'Yes' && weed >= 5) {
        console.log("Transaction was successful!");
        // Update the balance
        var updBalance = document.getElementById('balance');
        updBalance.textContent = balance + 150;
    } else {
        console.log("Failed!")
    }
}

<!DOCTYPE html>
<html lang="en">
    <head>
        <title></title>
        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
        <div id="container">
            <header>
                <div class="dashboard">
                    <div id="name"></div>
                    <div id="balance"></div>
                    <div id="gear"></div>
                    <div id="sell">
                        <button id="sellButton" onlick="sellGear()">Sell?</button>
                    </div>
                </div>
            </header>
        </div>
    </body>
    <script src="js/global.js"></script>
</html>

1 个答案:

答案 0 :(得分:1)

以下是解决方案和建议: 尝试在HTML的末尾使用javascript代码。

<html lang="en">
    <head>
        <title></title>
        <link rel="stylesheet" href="css/normalize.css">
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
        <div id="container">
            <header>
                <div class="dashboard">
                    <div id="name"></div>
                    <div id="balance"></div>
                    <div id="gear"></div>
                    <div id="sell">
                        <button id="sellButton" onclick="return sellGear();">Sell?</button>
                    </div>
                </div>
            </header>
        </div>
    </body>
    <script src="js/global.js"></script>
</html>

<SCRIPT>
// Set global variables
var name;
var balance;
var weed;

// Ask the user his name for his character
var name = window.prompt("What is your name?", "Cap'n Grow");
var finalName = document.getElementById('name');
finalName.textContent = name;

// Set the balance to default
var balance = 100;
var FinalBalance = document.getElementById('balance');
FinalBalance.textContent = balance;

var weed = 10;
var FinalWeed = document.getElementById('gear');
FinalWeed.textContent = weed;

// Sell function
function sellGear() {
  var check = window.prompt("Are you sure you want to sell 5 bags?", "Yes");
    if (check === 'Yes' && weed >= 5) {
        console.log("Transaction was successful!");
        // Update the balance
        var updBalance = document.getElementById('balance');
        updBalance.textContent = balance + 150;
    } else {
        console.log("Failed!")
    }
}
</SCRIPT>