使用Javascript和CSS生成博客文章

Generate Blogposts using Javascript and CSS

本文关键字:文章 Javascript CSS 使用      更新时间:2023-09-26

我想使用Javascript生成简单的博客文章。我的功能如下:

function generatePost(title, time, text) {
    var div = document.createElement("div");
    div.className = "content";
    document.getElementById("postcontainer").appendChild(div);
}

如果在HTML文档中使用我的函数,则结构应该如下所示:

<div id="postcontainer">
    <div class="content"> /*This is the actual post*/
        <h3>title<span>time</span></h3>
        <p>text</p>
    </div>
    <div class="content">
        <h3>title2<span>time2</span></h3>
        <p>text2</p>
    </div>
</div>

我不知道如何在类/ID不相互干扰的情况下生成多个帖子。知道吗?提前感谢!

这里有一个非常简单的解决方案。为每个内容div添加一个id,其中包括一个唯一的帖子id。在本例中,是帖子编号。

var postCount = 0;
function generatePost(title, time, text) {
    var div = document.createElement("div");
    div.className = "content";
    div.id = "blog_" + postCount
    postCount++;
   document.getElementById("postcontainer").appendChild(div);
}
相关文章: