为什么我得到Uncaught TypeError:无法读取属性' style'在这里未定义

时间:2016-08-20 09:01:32

标签: javascript html html5 ejs

我试图隐藏此表中的空白行。该表一次最多可以显示4行,所以如果count等于2,我只想要显示2行......我必须在这里遗漏一些非常明显的东西。我在Stackoverflow和其他论坛上回顾了所有类似的问题,但没有提供答案......任何人都可以看到这里有什么问题。顺便说一句......解决方案不是为元素设置ID并执行getElementById ..我试过了。我实际上是在res.render()中传递了 count 变量...但为方便起见,这里进行了硬编码。我将不胜感激任何帮助。

var quiz = [["When is Bulgaria established?", 681], ["What year was it before 16 years?", 2000], ["When does WWII ends?", 1945]],
    answer = [],
    correct = [],
    wrong = [],
    correctAns = 0,
    wrongAns = 0;

//function to print the result in ordered list
function printResult(result) {
    var oList = "<ol>"; // !!! move variable inside of the function
    for (var j = 0; j < result.length; j++) {
        oList += "<li>" + result[j] + "</li>"; // !!! use j indstead if i
    }
    oList += "</ol>";
    return oList;
}

function print(message) {
    document.getElementById('output').innerHTML += message; // !!! append message
}

//looping, adding correct and wrong answeres
for (var i = 0; i < 3; i++) {
    answer[i] = prompt(quiz[i][0]);
    if (parseInt(answer[i]) == quiz[i][1]) {
        correct.push(quiz[i][0]);
        correctAns++;
    } else {
        wrong.push(quiz[i][0]);
        wrongAns++;
    }
}

//print logic
if (correct.length < 1 || correct == undefined) {
    print("You did not guess any of the quiestions!");
} else if (correct.length >= 1) {
    print("You have guessed " + correctAns + " questions.");
    print(printResult(correct));
    print("You have " + wrongAns + " wrong answeres.");
    if (wrongAns > 0) {
        print(printResult(wrong));
    }
}

2 个答案:

答案 0 :(得分:1)

更新:JSFIDDLE您可以在此处通过更改tr变量来显示要显示的count代码。因此,如果count等于3,则只显示3 tr

答案 1 :(得分:0)

你应该使用activerows长度作为for的限制。可能你正在阅读数组实际拥有的更多元素