2

我是 javascript 和 html 的新手,所以我希望有人可以查看我的代码并告诉我为什么某些语法不起作用。在下面的代码中,我使用 document.getElementById 来检索我每次都想使用的元素。我确实将它们声明为 gameTime() 中的局部变量,并在整个函数中使用它。

    var randNum = Math.floor( 100*Math.random() ) +1;
    var numGuesses = 0;

    function gameTime()
    {   
        var guess = document.getElementById("guess").value;
        var status = document.getElementById("status"); 

        numGuesses++;

        if(guess == "")
        {
            alert("You did not guess a number!")
            numGuesses--;
        }
        else if(guess == randNum)
            status.value += (guess + " was the right number! It only took you " + numGuesses + " tries" + "\r")
        else if(guess > randNum)
            status.value += (guess + " is too high!" + "\r")
        else    
            status.value += (guess + " is too low!" + "\r")

        document.getElementById("guess").value = "";
    }

    function reset()
    {
        randNum = Math.floor( 100*Math.random() ) +1;
        numGuesses = 0;
        document.getElementById("guess").value = "";
        document.getElementById("status").value = "";
    }

    function quit()
    {
        document.getElementById("status").value += ("The correct number was " + randNum + "!\r")
    }

然后我通过全局声明这两个元素来尝试它,并在每个似乎根本不起作用的函数中使用它,我只是不明白为什么。

    var randNum = Math.floor( 100*Math.random() ) +1;
    var numGuesses = 0;
    var guess = document.getElementById("guess").value;
    var status = document.getElementById("status");

    function gameTime()
    {   
        numGuesses++;

        if(guess == "")
        {
            alert("You did not guess a number!")
            numGuesses--;
        }
        else if(guess == randNum)
            status.value += (guess + " was the right number! It only took you " + numGuesses + " tries" + "\r")
        else if(guess > randNum)
            status.value += (guess + " is too high!" + "\r")
        else    
            status.value += (guess + " is too low!" + "\r")

        guess.value = "";
    }

    function reset()
    {
        randNum = Math.floor( 100*Math.random() ) +1;
        numGuesses = 0;
        guess.value = "";
        status.value = "";
    }

    function quit()
    {
        status.value += ("The correct number was " + randNum + "!\r")
    }

这是这些js文件附带的html文件guess.js是第一个代码块,guessAgain.js是第二个代码块。

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Guessing Game</title>
            <script type="text/javascript" src="guessAgain.js"></script>
        </head>
        <body>
            <label for = "guess"> Your Guess: </label>
            <input type = "text" id = "guess" value = "" />    
            <input type = "button" onclick = "gameTime()" value = "Submit" />  
            <input type = "button" onclick = "reset()" value = "New Game" />
            <input type = "button" onclick = "quit()" value = "Quit" />
            <br>    
            <textarea id = "status" rows = "10" cols = "52"></textarea>  
        </body> 
    </html>

感谢您能给我的任何见解。

4

2 回答 2

1

您正在将guess 设置为 element.value

它应该是

var guess = document.getElementById("guess");

问题是您在guess这里用作元素:

guess.value = "";

您还需要将guess用作字符串的位置修改为guess.value.

于 2012-08-04T05:31:48.797 回答
1

问题 1: 您在正文加载之前调用以下内容,因此它们返回未定义。

var guess = document.getElementById("guess");
var status = document.getElementById("status");

您应该仅在主体加载onload()事件后执行这些操作。

问题 2:

此外,当您将值本身分配给变量时,它不会像文本框那样更新。var guess = document.getElementById("guess").value,那么guess如果您更改文本框,则 的值不会更新。

如果您参考var guess = document.getElementById("guess"),那么,重复调用 guess.value将返回输入字段的最新值。

(工作示例)


以下是没有错的不必要的复杂

        numGuesses++;   
        if(guess.value == "")
        {
            alert("You did not guess a number!")
            numGuesses--;
        }
        ...

相反,一旦它被认为是一个有效的猜测,你可以增加 numGuesses,即在最后。

于 2012-08-04T05:40:33.297 回答