簡體   English   中英

Javascript-無法獲取函數來設置要在函數外部使用的全局變量

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

我正在嘗試制作一個簡單的線性文本游戲,該游戲將在網頁上的div中顯示。 我正在使用innerHTML將游戲的內容寫入div,並使用按鈕上的onclick來更改內容。 我的問題是,我還想包括一些用戶提交的變量,我正在嘗試在函數內部使用hint()來完成這些操作。

問題是,我無法全局設置變量。 當在函數內部調用時,它將起作用,但在其他地方則無效。

我嘗試過使用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()函數之一)

無論如何,它永遠不會這樣工作。 目前next5初始化值player1定義一定的方式,除非你重新定義將保持定義的方式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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM