未捕获的typeError undefined不是函数

时间:2014-08-16 19:17:23

标签: javascript function undefined

我调试了很多代码,但我仍然遇到问题。这是课堂作业,我问我的教授,他也被震惊了。我确信修复此错误将有助于我的代码,但我错了我不确定。你能帮忙吗?这是错误

enter image description here

这是我的HTML代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Magic Word - Sample</title>
    <script src="magicWord.js"></script>
</head>
<body>
    <div>
        <h2 align="center">WELCOME TO MAGIC WORD!</h2>
        <h3>Please enter your guess on the space below:</h3>
        <div>
            <input type="text" id="guestGuess" />
            <input type="submit" id="submitButton" onclick="javascript:enteredGuess();" />
        </div>
    </div>
    <div>
        <h3>Your guesses so far are: </h3>
        <div id="userGuesses"></div>
        <div>You have guessed: <span id="userAttempts"></span> times</div>
    </div>
</body>
</html>

这是我的Javascript

var numOfAttempts = 5;
var numOfGuesses = 0;
var magicWord = "Just";

function guessesUsed(attemptsTaken) {
    var numGuessesLeft = document.getElementById("userAttempts");
    numGuessesLeft.innerHTML = attemptsTaken;
}

//function guessesLeft(attemptsLeft) {
//  var numGuessesUsed = document.getElementById("guessesLeft");
//  numGuessesUsed.innerHTML = attemptsLeft;    
//}

function listWordsUsed(wordUsed) {
    var userTrials = document.getElementbyId("userGuesses").innerText;
    var divisor = document.createElement("div");
    divisor.innerHTML = "<div id=" + wordUsed + ">" + wordUsed + "</div>";
    userTrials.appendChild(divisor);
    return;
}

function enteredGuess() 
{
    //A user will enter a word and the word will be checked against the magic word
    //var enterGuess = prompt("What is your guess?", "Enter your guess here");
    var theGuess = document.getElementById("guestGuess");
    var magicWordResult;

    // Used lower case for both instances to compare both words
    if (theGuess.value.toLowerCase() == magicWord.toLowerCase()) 
    {
        //The user guesses the correct word
        magicWordResult = "Your guess was" + theGuess + "! Nice going!";
        //document.writeln("Your guess was: " + guestGuess + "\nand the magic word is " + magicWord);
    }
    else  //The user guesses wrong
    {
        //When user gets it wrong increase by one the numOfGuesses
        numOfGuesses ++;
        magicWordResult = "Your guess was" + theGuess + ". Nice try, but that's wrong, try again. \nYou have used " + numOfGuesses + " guesses.";

        // Subtract the amount of guesses from the amount of attempts
        var guessesLeft = numOfAttempts - numOfGuesses;

        if (numOfGuesses == numOfAttempts)
        {
            // When the user has reached all its attemps
            magicWordResult = "Sorry, you ran out of guesses. The magic word was " + magicWord + ".";
        }
        else
        {
            magicWordResult = "You still have " + guessesLeft + " guesses left.";
        }

        //To show the number of guesses the user has used
        guessesUsed(numOfGuesses);

        //To show the number of guesses the user has left
        //guessesLeft(guessesLeft);

        //Creates a list of the words used by the user
        listWordsUsed(theGuess);

    }

    // Display in an alert mode and show how the user is doing
    alert(magicWordResult);
}

我的错误在哪里?

1 个答案:

答案 0 :(得分:2)

此:

var userTrials = document.getElementbyId("userGuesses").innerText;

将“userGuesses”元素的文本作为字符串获取,但是在这里:

userTrials.appendChild(divisor);

您的代码将其视为DOM节点。因为String实例上没有“appendChild”属性,它是undefined,这解释了错误 - 您正在尝试进行函数调用,但函数引用是undefined

编辑 - 哦,“getElementById()”中的拼写错误也很重要,正如crclayton在评论中所指出的那样。我错过了那个:)处理

的一般技巧
  

undefined不是函数

错误是尝试找到代码可能出现undefined的地方而不是对函数的引用。根据我的经验,主要原因是:

  • 错别字,就像“getElementbyId”一样。如果函数名拼写错误,JavaScript将无法理解,因此无法提供更好的错误。
  • 关于变量或属性引用的不满意的假设,如userTrials变量。
  • 假设某个函数返回对函数的引用,实际上(可能)返回undefined