Javascript 我在使用createElement(“ul”)创建新元素时遇到appendChild()问题
我在p标签下创建UL和LI时遇到问题。有人能看看我下面的代码吗?javascript返回以下错误: 未捕获类型错误:ThisQuizContainer.appendChild不是函数 将其更改为document.body.appendChild将在主体中创建UL。如何在div quizmain容器中创建UL 这是HTML:Javascript 我在使用createElement(“ul”)创建新元素时遇到appendChild()问题,javascript,html,Javascript,Html,我在p标签下创建UL和LI时遇到问题。有人能看看我下面的代码吗?javascript返回以下错误: 未捕获类型错误:ThisQuizContainer.appendChild不是函数 将其更改为document.body.appendChild将在主体中创建UL。如何在div quizmain容器中创建UL 这是HTML: <div id="quizMainContainer"> <h2>Question 1 of 3:</h2>
<div id="quizMainContainer">
<h2>Question 1 of 3:</h2>
<p id="quizQuestion"></p>
<!-- create UL here -->
</div>
这是我的Javascript
var ulCreate = document.createElement("ul");
var thisIsQuizContainer = document.querySelectorAll('#quizMainContainer');
function render(questionIndex) {
// Clears existing data
questionsDiv.innerHTML = "";
ulCreate.innerHTML = "";
thisIsQuizContainer.innerHTML = "";
// For loops to loop through all info in array
for (var i = 0; i < questions.length; i++) {
// Appends question title only
var userQuestion = questions[questionIndex].title;
var userChoices = questions[questionIndex].choices;
questionsDiv.textContent = userQuestion;
}
// New for each for question choices
userChoices.forEach(function (newItem) {
var listItem = document.createElement("li");
listItem.textContent = newItem;
thisIsQuizContainer.appendChild(ulCreate);
ulCreate.appendChild(listItem);
listItem.addEventListener("click", (compare));
})
}
我建议您使用从dom中选择元素的列表
document.getElementById('#quizMainContainer') // node element
ThisQuizContainer是一个节点列表,而不是一个使用元素的文档;而不是在此IsquizContainer之后调用Renderad[0]的用户。这是QuizContainer[0]。或者只通过id文档调用getElementById'quizMainContainer'getElementById需要这个散列吗@vamshi@Rafv,您需要使用来选择具有id属性的元素。和“.”用于选择类属性。
document.getElementById('#quizMainContainer') // node element