我正在进行测验,其中文本(问题,答案等)将加载javaScript。
到目前为止,我已经创建了两个按钮,(一个向前,另一个向后);而且我希望这些按钮在点击时加载我的问题......
这是数组:
var allQuestions = [{
"question": "Who was Luke's wingman in the battle at Hoth?",
"choices": ["Dak", "Biggs", "Wedge", "fx-7"],
"correctAnswer": 0
}, {//question #2 }, {//question #3 etc},...];
这是我的Js:
var button = document.querySelector('.navForward');
button.addEventListener('click', function() {
var questionOutput = " ";
var currentQuestion = 0;
var item = allQuestions;
if(currentQuestion < item.length){
currentQuestion++;
}
questionOutput += "<h4>" + item.question[currentQuestion] + "</h4> <br/>";
var update = document.getElementById("question");
update.innerHTML = questionOutput;
}, false);
有人可以解释我在控制台中遇到的这个错误吗?
Uncaught TypeError: Cannot read property '1' of undefined script.js:84 (anonymous function)
答案 0 :(得分:2)
question
对象中没有item
属性,因为它是一个数组。
将item.question[currentQuestion]
更改为item[currentQuestion].question
。