HTML文件不会显示我的javascript变量

时间:2014-09-27 19:40:10

标签: javascript html

我已经用一些变量编写了一个javascript函数,我试图测试它是否会在我的HTML文档中显示变量,但它们不会,我不知道为什么。具体来说,我正在尝试插入变量currentScore,它在开头设置为0,因此它应该在文本框中显示为0,但它不会出现在那里。

这是我的javascript:

var who = 0;
var decision = 0;
var diceOne = 0;
var diceTwo = 0;
var currentScore = 0;
player playerAI = new player;
player playerOne = new player;
document.getElementById('currentScore').value = currentScore;

function rollDice() {
diceOne = Math.round(6 * Math.Random() + 1);
diceTwo = Math.round(6 * Math.Random() + 1);
}
function mainFunction() {
    playerAI.playing = true;
    playerOne.playing = true;
    while (playerAI.playing == true && playerOne.playing == true) {
        makeMove();
    }
}
function makeMove() {
    if (who == 0) {
        aiStrat();
        game();
    }
    else {
        game();
    }
}
function game() {
    if (decision == 1) {
        rollDice();
        if (diceOne != 1 && diceTwo != 1){
            currentScore += diceOne + diceTwo;
            decision = 0;
            makeMove();
        }
        if (diceOne == 1 || diceTwo == 1){
            currentScore = 0;
            decision = 0;
            who = 1 - who;
            makeMove();
        }
        if (diceOne == 1 && diceTwo == 1) {
            currentScore = 0;
            if (who == 0) {
                playerAI.totalScore = 0;
            }
            else {
                playerOne.totalScore = 0;
            }
            decision = 0;
            who = 1 - who;
            makeMove();
        }
    }
    if(decision == -1) {
        if (who == 0){
            playerAI.totalScore += currentScore;
            playerAI.checkScore();
        }
        else {
            playerOne.totalScore += currentScore;
            playerOne.checkScore();
        }
        currentScore = 0;
        decision = 0;
        who = 1 - who;
    }       
}

function aiStrat() {
    if (playerAI.totalScore < 60) {
        if (currentScore < 30) {
            decision = 1;
        }
        else {
            decision = -1;
        }
    }
    if (playerAI.totalScore >= 60 && playerAI.totalScore < 80) {
        if (currentScore < 20){
            decision = 1;
        }
        else {
            decision = -1;
        }   
    }
    if (playerAI.totalScore >= 80){
        if (currentScore < 10) {
            decision = 1;
        }
        else {
            decision = -1;
        }
    }
}


var player {
    var totalScore = 0;
    var playing = true;
    function checkScore() {
        if (totalScore >= 100) {
            playing = false;
        }
    };
};

我的HTML文档是这样的:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="BigPig.css">
        <script type="text/javascript" src="VorgurakendusedKD1\Vorgurakendused.js" ></script>
    </head>
    <body onload="javascript:mainFunction()">

        <div class="centered" type="text/javascript">

            <h1>BIG PIG</h1>
            <button><input type="button" value="START FROM BEGINNING" onclick="mainFunction();">
            <span></span></button>
            <br>
            <button><span>GREEN BUTTON</span></button>
            <br>
            <button><span>RED BUTTON</span></button>
            <br>
            <output class="textbox" type="text/javascript" id="currentScore">CURRENTSCORE:         
            </output>
            <br>
            <output class="textbox" type="text">CPU SCORE: </output>
            <br>
            <output class="textbox" type="text">PLAYER SCORE: </output>
            <br>
            <p>Player versus computer</p>
            <br>
            <p id="currentScore"></p>
        </div>
    </body>
</html> 

1 个答案:

答案 0 :(得分:0)

此处:document.getElementById('currentScore').value = currentScore;您尝试在加载元素之前找到该元素,这就是您无法为其指定值的原因。

尝试将document.getElementById('currentScore').value = currentScore;放入onload-function,mainFunction()