gpt4 book ai didi

javascript - HTMLButtonElement.onclick上未捕获的ReferenceError

转载 作者:行者123 更新时间:2023-12-03 08:38:54 25 4
gpt4 key购买 nike

我在html和js中有一个测验代码。测验有10个问题,应该有在两个问题之间移动的按钮。
第一个问题工作正常,但是当我尝试回答第二个问题时,我在控制台中收到此错误:
Game.html:1未捕获的ReferenceError:未定义num
在HTMLButtonElement.onclick(Game.html:1)
我该怎么做才能防止此错误?我试图将js代码复制到html中的script元素,但是没有用。
html的代码如下所示:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Game</title>
<script src="script.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body id="gameBody" onload="InitGame()">

<h1>Welcome to the game</h1>
<h3 class="header">Your score</h3>
<h3 class="text" id="score"></h3>
<h5 class="header">Choose the right answer</h5>
<p class="header">The question number</p>
<p class="text" id="questionNumber"></p>
<p class="header">The word you need to translate</p>
<p id="word"></p>
<div id="image">

</div>
<div id="options">

</div>
<p id="message"></p>
<button class="myChoise" onclick="prevQuestion()">Previous</button>
<button class="myChoise" onclick="nextQuestion()">Next</button>

</body>
</html> ```

And in script.js I have this array of objects :

let food=[{
'ID':1,
'word':'What is the national dish of Italy',
'options':[
{name : 'פסטה, פיצה, ריזוטו'},
{name : 'המבורגר'},
{name : 'מרק עוף'}
],
'img':'img/200.jpg',
'rightAnswer':'פסטה, פיצה, ריזוטו',
},
{
'ID':2,
'word':'What is the national dish of the United States?',
'options':[
{name : 'המבורגר, נקנקיה בלחמניה, פאי '},
{name : 'פיצה'},
{name : 'ריזוטו'}
],
'img':'img/23.jpg',
'rightAnswer':'המבורגר, נקנקיה בלחמניה, פאי',
},
{
'ID':3,
'word':'What is the national dish of Hungary?',
'options':[
{name : 'גולאש'},
{name : 'המבורגר'},
{name : 'נקנקיה בלחמניה'}
],
'img':'img/21.jpg',
'rightAnswer':'גולאש',
},
{
'ID':4,
'word':'What is the national dish of Greece?',
'options':[
{name : 'מוסקה,סלט יווני'},
{name : 'גולאש'},
{name : 'המבורגר'}
],
'img':'img/222.jpg',
'rightAnswer':'סלט יווני,מוסקה',
},
{
'ID':5,
'word':'What is the national dish of Belarus?',
'options':[
{name : 'לביבה'},
{name : 'קציצות בשר'},
{name : 'מרק עוף'}
],
'img':'img/444.jpg',
'rightAnswer':'לביבה',
},
{
'ID':6,
'word':'What is the national dish of the United Kingdom?',
'options':[
{name : ' פיש אנד ציפס'},
{name : 'ответ'},
{name : 'название'}
],
'img':'img/20.jpg',
'rightAnswer':' פיש אנד ציפס',
},
{
'ID':7,
'word':'What is China national dish?',
'options':[
{name : 'אורז'},
{name : 'קציצות בקר'},
{name : 'המבורגר'}
],
'img':'img/486.jpg',
'rightAnswer':'אורז',
},
{
'ID':8,
'word':'What is the national dish of France?',
'options':[
{name : 'באגט, קרואסון, פואה גרא'},
{name : 'נקנקיה בלחמניה'},
{name : 'לביבות'}
],
'img':'img/22.jpg',
'rightAnswer':'באגט, קרואסון, פואה גרא',
},
{
'ID':9,
'word':'What is the national dish of Cyprus?',
'options':[
{name : 'חלומי'},
{name : 'באגט'},
{name : 'אורז'}
],
'img':'img/24.jpg',
'rightAnswer':'חלומי',
},
{
'ID':10,
'word':'What is the national dish of Mexico?',
'options':[
{name : 'טאקו, גואקמולי'},
{name : 'אורז'},
{name : 'פיש אנד ציפס'}
],
'img':'img/264.jpg',
'rightAnswer':'טאקו,גואקמולי',
}
]


And this functions :

let score = 0;
function InitGame(){
document.getElementById("questionNumber").innerHTML = food[0].ID;
document.getElementById("score").innerHTML= score;
document.getElementById("word").innerHTML= food[0].word;
let imgSrc = food[0].img;
let img = document.createElement("img");
img.src = food[0].img;
img.width = '500';
img.height = '300';
document.getElementById("image").appendChild(img);
document.getElementById("options").innerHTML =
"<button class='btn' id='1' onclick='checkAnswer(food[0].ID,this)'></button> " +
"<button class='btn' id='2' onclick='checkAnswer(food[0].ID,this)'></button> " +
"<button class='btn' id='3' onclick='checkAnswer(food[0].ID,this)'></button>";
document.getElementById("options").getElementsByTagName('button')[0].innerHTML = food[0].options[0].name;
document.getElementById("options").getElementsByTagName('button')[1].innerHTML = food[0].options[1].name;
document.getElementById("options").getElementsByTagName('button')[2].innerHTML = food[0].options[2].name;
}

function checkAnswer(questionNum,btn){
for(i in food)
{
if(food[i].ID === questionNum)
{
let chosed = btn.id;
let answer = food[i].options[chosed-1].name;
if(answer === food[i].rightAnswer)
{
score = score+1;
document.getElementById("score").innerHTML= score;
document.getElementById("message").innerHTML = "Right answer";
nextQuestion();
}
else
{
score = score-0.5;
document.getElementById("score").innerHTML= score;
document.getElementById("message").innerHTML = "wrong answer, please try again";
}
}
}
}

function nextQuestion(){
let qNum = document.getElementById("questionNumber").innerHTML;
let num = ++qNum;
if(num === 10)
{
num = 0;
}
document.getElementById("questionNumber").innerHTML = num;
document.getElementById("score").innerHTML= score;
document.getElementById("word").innerHTML= food[num].word;
document.getElementById("image").innerHTML="";
let imgSrc = food[num].img;
let img = document.createElement("img");
img.src = food[num].img;
img.width = '500';
img.height = '300';
document.getElementById("image").appendChild(img);
document.getElementById("options").innerHTML="";
document.getElementById("options").innerHTML =
"<button class='btn' id='1' onclick='checkAnswer(food[num].ID,this)'></button> " +
"<button class='btn' id='2' onclick='checkAnswer(food[num].ID,this)'></button> " +
"<button class='btn' id='3' onclick='checkAnswer(food[num].ID,this)'></button>";
document.getElementById("options").getElementsByTagName('button')[0].innerHTML = food[num].options[0].name;
document.getElementById("options").getElementsByTagName('button')[1].innerHTML = food[num].options[1].name;
document.getElementById("options").getElementsByTagName('button')[2].innerHTML = food[num].options[2].name;

}


function prevQuestion(){
let qNum = document.getElementById("questionNumber").innerHTML;
let num = --qNum;
if(num === -1)
{
num = 10;
}
document.getElementById("questionNumber").innerHTML = num;
document.getElementById("score").innerHTML= score;
document.getElementById("word").innerHTML= food[num].word;
document.getElementById("image").innerHTML="";
let imgSrc = food[num].img;
let img = document.createElement("img");
img.src = food[num].img;
img.width = '500';
img.height = '300';
document.getElementById("image").appendChild(img);
document.getElementById("options").innerHTML="";
document.getElementById("options").innerHTML =
`<button class='btn' id='1' onclick='checkAnswer(food[num].ID,this)'></button> <button class='btn' id='2' onclick='checkAnswer(food[num].ID,this)'></button> <button class='btn' id='3' onclick='checkAnswer(food[num].ID,this)'></button>`;
document.getElementById("options").getElementsByTagName('button')[0].innerHTML = food[num].options[0].name;
document.getElementById("options").getElementsByTagName('button')[1].innerHTML = food[num].options[1].name;
document.getElementById("options").getElementsByTagName('button')[2].innerHTML = food[num].options[2].name;
}

最佳答案

您获取num is not defined的原因是,您在这种情况下设置了像'checkAnswer(food[num].ID,this)'这样的内部HTML,因此在函数中数字未引用num变量,而是应使用'checkAnswer(food[" + num + "].ID,this)',以便当html呈现而不是num时,它将像这个checkAnswer(food[9].ID,this)

let food = [{
'ID': 1,
'word': 'What is the national dish of Italy',
'options': [{
name: 'פסטה, פיצה, ריזוטו'
},
{
name: 'המבורגר'
},
{
name: 'מרק עוף'
}
],
'img': 'img/200.jpg',
'rightAnswer': 'פסטה, פיצה, ריזוטו',
},
{
'ID': 2,
'word': 'What is the national dish of the United States?',
'options': [{
name: 'המבורגר, נקנקיה בלחמניה, פאי'
},
{
name: 'פיצה'
},
{
name: 'ריזוטו'
}
],
'img': 'img/23.jpg',
'rightAnswer': 'המבורגר, נקנקיה בלחמניה, פאי',
},
{
'ID': 3,
'word': 'What is the national dish of Hungary?',
'options': [{
name: 'גולאש'
},
{
name: 'המבורגר'
},
{
name: 'נקנקיה בלחמניה'
}
],
'img': 'img/21.jpg',
'rightAnswer': 'גולאש',
},
{
'ID': 4,
'word': 'What is the national dish of Greece?',
'options': [{
name: 'מוסקה,סלט יווני'
},
{
name: 'גולאש'
},
{
name: 'המבורגר'
}
],
'img': 'img/222.jpg',
'rightAnswer': 'מוסקה,סלט יווני',
},
{
'ID': 5,
'word': 'What is the national dish of Belarus?',
'options': [{
name: 'לביבה'
},
{
name: 'קציצות בשר'
},
{
name: 'מרק עוף'
}
],
'img': 'img/444.jpg',
'rightAnswer': 'לביבה',
},
{
'ID': 6,
'word': 'What is the national dish of the United Kingdom?',
'options': [{
name: ' פיש אנד ציפס'
},
{
name: 'ответ'
},
{
name: 'название'
}
],
'img': 'img/20.jpg',
'rightAnswer': ' פיש אנד ציפס',
},
{
'ID': 7,
'word': 'What is China national dish?',
'options': [{
name: 'אורז'
},
{
name: 'קציצות בקר'
},
{
name: 'המבורגר'
}
],
'img': 'img/486.jpg',
'rightAnswer': 'אורז',
},
{
'ID': 8,
'word': 'What is the national dish of France?',
'options': [{
name: 'באגט, קרואסון, פואה גרא'
},
{
name: 'נקנקיה בלחמניה'
},
{
name: 'לביבות'
}
],
'img': 'img/22.jpg',
'rightAnswer': 'באגט, קרואסון, פואה גרא',
},
{
'ID': 9,
'word': 'What is the national dish of Cyprus?',
'options': [{
name: 'חלומי'
},
{
name: 'באגט'
},
{
name: 'אורז'
}
],
'img': 'img/24.jpg',
'rightAnswer': 'חלומי',
},
{
'ID': 10,
'word': 'What is the national dish of Mexico?',
'options': [{
name: 'טאקו,גואקמולי'
},
{
name: 'אורז'
},
{
name: 'פיש אנד ציפס'
}
],
'img': 'img/264.jpg',
'rightAnswer': 'טאקו,גואקמולי',
}
]

let score = 0;

function InitGame() {
document.getElementById("questionNumber").innerHTML = food[0].ID;
document.getElementById("score").innerHTML = score;
document.getElementById("word").innerHTML = food[0].word;
let imgSrc = food[0].img;
let img = document.createElement("img");
img.src = food[0].img;
img.width = '500';
img.height = '300';
document.getElementById("image").appendChild(img);
document.getElementById("options").innerHTML =
"<button class='btn' id='1' onclick='checkAnswer(food[0].ID,this)'></button> " +
"<button class='btn' id='2' onclick='checkAnswer(food[0].ID,this)'></button> " +
"<button class='btn' id='3' onclick='checkAnswer(food[0].ID,this)'></button>";
document.getElementById("options").getElementsByTagName('button')[0].innerHTML = food[0].options[0].name;
document.getElementById("options").getElementsByTagName('button')[1].innerHTML = food[0].options[1].name;
document.getElementById("options").getElementsByTagName('button')[2].innerHTML = food[0].options[2].name;
}

function checkAnswer(questionNum, btn) {
debugger
for (i in food) {
if (food[i].ID === questionNum) {
let chosed = btn.id;
let answer = food[i].options[chosed - 1].name;
if (answer === food[i].rightAnswer) {
score = score + 1;
document.getElementById("score").innerHTML = score;
document.getElementById("message").innerHTML = "Right answer";
nextQuestion();
} else {
score = score - 0.5;
document.getElementById("score").innerHTML = score;
document.getElementById("message").innerHTML = "wrong answer, please try again";
}
}
}
}

function nextQuestion() {
let qNum = document.getElementById("questionNumber").innerHTML;
let num = ++qNum;
if (num === 10) {
num = 0;
}
document.getElementById("questionNumber").innerHTML = num;
document.getElementById("score").innerHTML = score;
document.getElementById("word").innerHTML = food[num].word;
document.getElementById("image").innerHTML = "";
let imgSrc = food[num].img;
let img = document.createElement("img");
img.src = food[num].img;
img.width = '500';
img.height = '300';
document.getElementById("image").appendChild(img);
document.getElementById("options").innerHTML = "";
document.getElementById("options").innerHTML =
"<button class='btn' id='1' onclick='checkAnswer(food[" + num + "].ID,this)'></button> " +
"<button class='btn' id='2' onclick='checkAnswer(food[" + num + "].ID,this)'></button> " +
"<button class='btn' id='3' onclick='checkAnswer(food[" + num + "].ID,this)'></button>";
document.getElementById("options").getElementsByTagName('button')[0].innerHTML = food[num].options[0].name;
document.getElementById("options").getElementsByTagName('button')[1].innerHTML = food[num].options[1].name;
document.getElementById("options").getElementsByTagName('button')[2].innerHTML = food[num].options[2].name;
debugger;
}


function prevQuestion() {
let qNum = document.getElementById("questionNumber").innerHTML;
let num = --qNum;
if (num === -1) {
num = 10;
}
document.getElementById("questionNumber").innerHTML = num;
document.getElementById("score").innerHTML = score;
document.getElementById("word").innerHTML = food[num].word;
document.getElementById("image").innerHTML = "";
let imgSrc = food[num].img;
let img = document.createElement("img");
img.src = food[num].img;
img.width = '500';
img.height = '300';
document.getElementById("image").appendChild(img);
document.getElementById("options").innerHTML = "";
document.getElementById("options").innerHTML =
`<button class='btn' id='1' onclick='checkAnswer(food[` + num + `].ID,this)'></button> <button class='btn' id='2' onclick='checkAnswer(food[` + num + `].ID,this)'></button> <button class='btn' id='3' onclick='checkAnswer(food[` + num + `].ID,this)'></button>`;
document.getElementById("options").getElementsByTagName('button')[0].innerHTML = food[num].options[0].name;
document.getElementById("options").getElementsByTagName('button')[1].innerHTML = food[num].options[1].name;
document.getElementById("options").getElementsByTagName('button')[2].innerHTML = food[num].options[2].name;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Game</title>
</head>

<body id="gameBody" onload="InitGame()">

<h1>Welcome to the game</h1>
<h3 class="header">Your score</h3>
<h3 class="text" id="score"></h3>
<h5 class="header">Choose the right answer</h5>
<p class="header">The question number</p>
<p class="text" id="questionNumber"></p>
<p class="header">The word you need to translate</p>
<p id="word"></p>
<div id="image">

</div>
<div id="options">

</div>
<p id="message"></p>
<button class="myChoise" onclick="prevQuestion()">Previous</button>
<button class="myChoise" onclick="nextQuestion()">Next</button>

</body>

</html>

关于javascript - HTMLButtonElement.onclick上未捕获的ReferenceError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64023541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com