有没有一种方法可以从对象生成新的HTML元素?

时间:2019-11-19 18:41:18

标签: javascript html css node.js electron

我正在开发一个Electron应用程序,它使用Reddit API(snoowarp模块)从我的subreddit中获取帖子作为对象。

在我的 index.html 中,我有一个div元素来显示这些帖子,现在我正在寻找一种将其生成为 HTML div元素,然后将其放置的方法在那里。

这是我的应用的图片:

This is a picture of my app

,谢谢:)

1 个答案:

答案 0 :(得分:1)

这是我为您解决的问题的解决方案。希望对您有所帮助:)

const container = document.querySelector("#container");
newDiv = document.createElement("div");
newDiv.textContent = "Hello";
newDiv.setAttribute("id", "red");
container.appendChild(newDiv);
#red{
  background:red;
}
<div id="container">
</div>

相关问题