JAVASCRIPT 变量在函数中没有变化

JAVASCRIPT variable no changing in a function

本文关键字:有变化 函数 变量 JAVASCRIPT      更新时间:2023-09-26

我似乎无法让函数工作。我正在用javascript编写一个基于网络文本的游戏。我想发生的是将 lootKey 设置为无,并在单击按钮但不工作时将密钥设置为"key{br}"。

这是我的代码。

var lootKey = "";
var key = "";
if statment {
    lootKey = "<button onclick='function take_key'>take</button>Key<br>";
    function take_key() {
        key = "key<br>";
        lootKey = "";
    }
} 

我可以看到您应该修改的几段代码才能使其正常工作:

var lootKey = "";
var key = "";
if (statment) { //wrap your statement in parentheses
    lootKey = "<button onclick='take_key()'>take</button>Key<br>";//use take_key() to call a function
    function take_key() {
        key = "key<br>";
        lootKey = "";
    }
} 

这里是代码

if (beginDone == true) { if (input == "GO") {

            var key = "";
            var lootKey = "";
            page = page + 1;
            healthPoints = healthPoints - 25;
            coins = coins + 25;
            lootKey = "<button onclick='function take_key()'>take</button>Key<br>";
            function take_key() {
                key = "key<br>";
                lootKey = "";
            }
            document.getElementById("hp").innerHTML = "Health Points: " + ("<a>" + healthPoints + "</a>");
            document.getElementById("coins").innerHTML = "coins: " + ("<a>" + coins + "</a>");
            document.getElementById("page").innerHTML = "Page: " + ("<a>" + page + "</a>");
            document.getElementById("chapter").innerHTML = "Chapter: " + ("<a>" + chapter + "</a>");
            document.getElementById("text_inventory").innerHTML = "Inventory: <br><br>" + "<a>" + key + sword + shield + "</a>";
            document.getElementById("text_loot").innerHTML = "Loot: <br><br>" + "<a>" + lootKey + "</a>";
            $("<p class='c_light_grey'>You lost 25 hp, gained a key and gained 25 coins. There is a key lying on the ground in front of you.</p>").hide().insertBefore("#placeholder").fadeIn(2000);
        }
    }