Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我在使用createElement(“ul”)创建新元素时遇到appendChild()问题_Javascript_Html - Fatal编程技术网

Javascript 我在使用createElement(“ul”)创建新元素时遇到appendChild()问题

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>

我在p标签下创建UL和LI时遇到问题。有人能看看我下面的代码吗?javascript返回以下错误:

未捕获类型错误:ThisQuizContainer.appendChild不是函数

将其更改为document.body.appendChild将在主体中创建UL。如何在div quizmain容器中创建UL

这是HTML:

<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