任何人都可以看到这个JavaScript代码有问题吗?

时间:2010-11-09 01:49:34

标签: javascript

我有下面的代码(我知道这只是一堆代码中的一个功能,但希望它已经足够了)。控制台为var userAnswer = currentBlank.value;提供错误“无法读取未定义的属性值”。适用的DOM(我认为DOM将比html源更有用)直接在下面(在图片中)和我遇到问题的javascript函数就在下面。我似乎无法弄清楚出了什么问题。想到也许新的眼睛可能会。

alt text

var allBlanksLoc = document.getElementById('text');

allBlanksLoc.style.display = 'block';

var allBlanks = allBlanksLoc.getElementsByTagName('input');

var sortedblanks = blanks.sort(function(a, b){
    return a - b
});
var wrongBlanks = 0; // logs how many blanks were answered wrongly over all
var rightBlanks = 0; // logs how many blanks were answered rightly over all

var testAnswersArea = document.getElementById('testAnswers');

finalAnswers = "";
var blankNum = 0;

for (var i = 0; i < wordlist.length; i++) {
    if (indicesOfBlanks[i]) {
        var currentBlank = allBlanks[blankNum];
        var wordNum = sortedblanks[blankNum];
        var blankWord = wordlist[wordNum];
        var blankWordUp = wordlistUpper[wordNum];
        var usrAnswer = currentBlank.value;
        if (usrAnswer != blankWord && usrAnswer != blankWordUp) {
            finalAnswers += "<del style=\"font: red;\">" + currentBlank.value + "</del> <span style=\"font: green;\">" + blankWordUp + "</span> ";
        } else {
            finalAnswers += "<span style=\"font: green;\">" + currentBlank.value + "<span>";
        }
        blankNum++;
    }
    else {
        finalAnswers += wordlistUpper[i] + " ";
    }
}

testAnswersArea.innerHTML = finalAnswers;

1 个答案:

答案 0 :(得分:2)

wordList的元素多于allBlanks 因此,allBlanks[blankNum]undefined