我正在设计一个测验应用程序来娱乐。测验将显示一个问题,并有四个选项可供回答。我使用 vanilla JS 来更新 DOM,并且需要为呈现的每个答案添加一个 eventListener。由于某种原因,一旦渲染 DOM,eventListener 就会触发。有什么想法吗?
//add each answer to the answerContainer
answerContainer.innerHTML = '';
for (let i = 0; i < allAnswersFlattened.length; i++) {
let individualAnswer = document.createElement('div');
individualAnswer.className = 'answer';
let answerText = document.createTextNode(allAnswersFlattened[i]);
individualAnswer.appendChild(answerText);
individualAnswer.addEventListener("click", checkForAnswer(i, allAnswersFlattened));
answerContainer.appendChild(individualAnswer);
}
慕姐8265434
慕村9548890
相关分类