Javascript 未捕获类型错误:answer.appendChild不是函数
这是我的全部代码:Javascript 未捕获类型错误:answer.appendChild不是函数,javascript,append,appendchild,Javascript,Append,Appendchild,这是我的全部代码: <div class="conteiner"> <h3 class="kitxva hide">Question</h3> <div class="questionCon " id="questionCon" > <button class="anwser1 ">Anwser1</button> <button class="anwser1 ">Anwser1&
<div class="conteiner">
<h3 class="kitxva hide">Question</h3>
<div class="questionCon " id="questionCon" >
<button class="anwser1 ">Anwser1</button>
<button class="anwser1 ">Anwser1</button>
<button class="anwser1 ">Anwser1</button>
<button class="anwser1 ">Anwser1</button>
<div class="controls">
<button id="start" class="anwser">Start</button>
<button id="next" class="anwser1">Next</button>
</div>
</div>
</div>
问题:
Anwser1
Anwser1
Anwser1
Anwser1
开始
下一个
const start=document.getElementById('start');
const next=document.getElementById('next');
const questionCon=document.getElementById('questionCon');
const kixtva=document.querySelector('h3')
const anwser=document.queryselectoral('.anwser1');
让我们来看看等式,currentQuestionIndex
start.addEventListener('click',startName');
函数startName(){
对于(var i=0;i Math.random()-.5)
currentQuestionIndex=0;
setNextQuestion()
}
函数setNextQuestion(){
重置状态()
showQuestion(ShuffeQuestions[currentQuestionIndex])
}
函数resetState(){
next.classList.add('hide')
while(anwser.firstChild){
anwser.removeChild(anwser.firstChild)
}
}
引用
answer.appendChild(按钮)时出错代码>我看不到答案代码>在您的代码中,但我看到answer
是document.querySelectorAll('.anwser1')
问题是querySelectorAll()
将所有元素作为NodeList
对象返回,而不仅仅是一个元素。您正在查找querySelector()
,它返回与指定CSS选择器匹配的第一个元素,或者如果您想使用querySelector()获取所有答案,您必须迭代NodeList
中的每个元素(您可以使用for
或forEach
或诸如此类).1)打字错误。你写的是“anwser”而不是“答案”。2) 在代码中,anwser
是一个节点列表。您需要确定要将子元素附加到的一个元素。3) 您的代码实际上不包括行answer.appendChild(按钮)
,因此可能不止这些问题。很难说,嗨。我已经修改了代码格式。格式化代码最简单的方法是在代码周围加上“`”。我也把缩进弄清楚了。请编辑该问题以包含您收到的错误,好吗?
const start = document.getElementById('start');
const next = document.getElementById('next');
const questionCon = document.getElementById('questionCon');
const kixtva = document.querySelector('h3')
const anwser = document.querySelectorAll('.anwser1');
let shuffleQuestions, currentQuestionIndex
start.addEventListener('click',startGame);
function startGame(){
for (var i=0;i<anwser.length;i+=1){
anwser[i].style.display = 'block';
}
kixtva.classList.remove('hide');
start.classList.add('hide')
shuffleQuestions = questions.sort(() => Math.random() - .5)
currentQuestionIndex = 0;
setNextQuestion()
}
function setNextQuestion(){
resetState()
showQuestion(shuffleQuestions[currentQuestionIndex])
}
function resetState(){
next.classList.add('hide')
while(anwser.firstChild){
anwser.removeChild(anwser.firstChild)
}
}