Javascript - 无法获取函数来设置要在函数外部使用的全局变量

Javascript - Cannot get function to set global variable to be used outside function

本文关键字:函数 外部 全局变量 设置 获取 Javascript      更新时间:2023-09-26

我正在尝试制作一个简单的线性文本游戏,该游戏将显示在网页上的div内。我正在使用 innerHTML 将游戏的内容写入div,并使用按钮的 onclick 来更改内容。我的问题是我还想包含一些用户提交的变量,我正在尝试在函数中使用 prompt() 执行此操作。

问题是,我无法全局设置变量。它在函数内部调用时有效,但在其他地方不起作用。

我尝试首先在函数外部声明变量,使用 window.variable(函数内部和外部),并在函数内的变量之前保留var以使其在范围内全局。

我一直在寻找解决方案,但似乎没有任何效果!我的脚本顺序是否遗漏了某些内容?

这是javascript:

var cb2 = '<input id="button" type="button" value="Continue" onclick="replace(''gamebox'',next3,''continueBttn'',cb3);">';
var cb3 = '<input id="button" type="button" value="Continue" onclick="getName();">';
var cb4 = '<input id="button" type="button" value="Continue" onclick="replace(''gamebox'',next5,''continueBttn'',cb5);">';
var cb5 = '<input id="button" type="button" value="Continue" onclick="replace(''gamebox'',next6,''continueBttn'',cb6);">';

var testName = "Test Name";
var player1;
var next2 = "<p>Great, you've certainly got an adventurer's spirit! Now I just need a few details about you and your party.</p>";
var next3 = "<p>First, I'd like to get everyone's name</p>"
var next4 = "<p>Thanks " + testName + "!</p>"
var next5 = "<p>Now you're ready " + player1 + "! Click to set out on the trail!</p>"
var continueButton = function (content) {
    document.getElementById('continueBttn').innerHTML = content;
    };
function replace(id1,content,id2,cb) {
    document.getElementById(id1).innerHTML = content;
    document.getElementById(id2).innerHTML = cb;
}
function getName() {
    player1 = prompt("What is your Name?");
    alert("Your name is " + player1 + ".");
    replace('gamebox',next4,'continueBttn',cb4);
}

这是 html:

<!DOCTYPE html>
<html>
    <head>
    <link rel="stylesheet" type="text/css" href="oregon.css" />
    </head>
    <body>
        <div id="gameContainer">
            <div id="gamebox">
            <p>Welcome to the Oregon Trail! Click Continue to travel the trail!</p>
            </div>
        </div>
    <div id="continueBttn"><input id="button" type="button" value="Continue" onclick="replace('gamebox',next2,'continueBttn',cb2);"></div>
    </body>

    </html>
    <script src="oregon.js" type="text/javascript"></script>

好的,我让你的东西工作了。 去除变量的var,使它们成为全局变量,并将函数更改为分配给变量,如 continueButton

replace = function(id1, content, id2, cb) {
    document.getElementById(id1).innerHTML = content;
    document.getElementById(id2).innerHTML = cb;
}
getName = function() {
    player1 = prompt("What is your Name?");
    alert("Your name is " + player1 + ".");
    replace('gamebox', next4, 'continueBttn', cb4);
}

这让事情对我有用。

这里的其他答案也有优点,您需要一种更好的方法来处理玩家名称。

player1用于表达式 next5 ,而不是 next4(这是您的getName()函数之一)

无论如何,它永远不会像这样工作。在初始化next5player1的值以某种方式定义,并且将保持这种方式定义,除非您再次重新定义next5变量。

您需要将next5定义封装到函数中,以使其动态化。

问题就在这里

var next5 = "<p>Now you're ready " + player1 + "! Click to set out on the trail!</p>"

它在首次呈现时使用该变量,当您将player1设置为新值时,它不会更新。

您需要找出一种不同的方法来设置玩家的名称。一种方法是更换它。

var next5 = "<p>Now you're ready {player1}! Click to set out on the trail!</p>"

当你使用它时

var newStr = next5.replace("{player1}", player1);