gpt4 book ai didi

javascript - 如何将javascript生成的新div插入现有的div?

转载 作者:行者123 更新时间:2023-11-28 04:58:29 25 4
gpt4 key购买 nike

JavaScript

 for (var i = 0; i < surveyResults.resultsOfSurvey.length; i = i + 1) {
debugger;

var answer = surveyResults.resultsOfSurvey[i].correct;
var qId = surveyResults.resultsOfSurvey[i].id;
var newElement = document.createElement('div');
newElement.id = surveyResults[i];
newElement.className = "answer";
newElement.innerHTML = "Antwort ist: " + answer + "! Fragen ID ist: " + qId + ".";
document.body.appendChild(newElement);

}

HTML

<div id="contentForSurvey">

<div id="answers"></div>

如何将 for 循环中的 div 插入到带有 id="answers" 的 div 中?

最佳答案

怎么样:

document.getElementById("answers").appendChild(newElement);

代替 document.body.appendChild(newElement);

关于javascript - 如何将javascript生成的新div插入现有的div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19053712/

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