仅在 Vanilla JavaScript 中附加在第一篇文章上的评论

我正在创建一个状态发布和评论系统。它是在 Vanilla JavaScript 中实现的。任何人都可以添加帖子并可以对帖子发表评论。一切正常,但评论部分仅针对第一篇文章。删除评论和帖子工作正常。我不知道问题出在哪里,如果有人能帮助我的话。这是 HTML 代码


 <div class="container-post" id="container-post">

    <div class="create-post">

        <form>

            <div class="form-group">

                <div class="username">

                    <p class="name" style="top:15px;">User Name</p>

                </div>

                <p class="qoutes">

                    <textarea style=" font-size: 15pt;" class="form-control" id="enter-post" rows="7" id="mypara" placeholder="Share Your Thoughts..."></textarea>

                </p>

                <div class="postbar">

                    <button type="button" class="btn btn-primary post-me" id="post-button"> <span id="postText">Post</span></button>

                </div>

            </div>

        </form>

    </div>

    <hr class="line-bar">


    <div class="allpost">


        <div class="mainpost" id="post-div"></div>

       

    </div>


精慕HU
浏览 148回答 2
2回答

天涯尽头无女友

当您使用如下代码时:createComment.innerHTML&nbsp;=&nbsp;commentHTMLValue;您正在完全替换元素的内容。尝试使用:createComment.innerHTML&nbsp;+=&nbsp;commentHTMLValue;它将新内容附加到现有内容的末尾。

红糖糍粑

我不能在这里做一个片段,因为不允许使用 localStorage。将此块复制到一个空白文件中并将其另存为 html 文件,然后在浏览器中打开它。这就是我认为您描述需求的方式,也是基于我评论中的数据格式。它不漂亮,需要大量修饰,但它可以运行。<!DOCTYPE html><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"><html><head><title>Task listing</title><script type="text/javascript">let taskList = [];function checkTasks() {&nbsp; let tasksList = getTasksList();&nbsp; if (tasksList.length == 0) {&nbsp; &nbsp; let newTask = prompt("Please enter a task description");&nbsp; &nbsp; if (newTask) {&nbsp; &nbsp; &nbsp; let thisIndex = getNewIndex();&nbsp; &nbsp; &nbsp; let a = {"id": thisIndex, "task": newTask, "comments": []}&nbsp; &nbsp; &nbsp; taskList.push(a);&nbsp; &nbsp; &nbsp; saveTasksList(taskList);&nbsp; &nbsp; }&nbsp; }&nbsp; displayTasks();}function displayTasks() {&nbsp; let container = document.getElementById("tasks");&nbsp; container.innerHTML = "";&nbsp; let taskList = getTasksList();&nbsp; taskList.forEach(function(task){&nbsp; &nbsp; let d = document.createElement("div");&nbsp; &nbsp; d.id = "task_" + task.id;&nbsp; &nbsp; d.className = "commentdiv";&nbsp; &nbsp; d.innerHTML = "<h3>" + task.task + "</h3>";&nbsp; &nbsp; let l = document.createElement("ul");&nbsp; &nbsp; l.id = "comments_" + task.id;&nbsp; &nbsp; let comments = task.comments;&nbsp; &nbsp; if (comments.length > 0) {&nbsp; &nbsp; &nbsp; let commentindex = 0;&nbsp; &nbsp; &nbsp; comments.forEach(function(comment) {&nbsp; &nbsp; &nbsp; &nbsp; let c = document.createElement("li");&nbsp; &nbsp; &nbsp; &nbsp; c.innerHTML = comment;&nbsp; &nbsp; &nbsp; &nbsp; let cb = document.createElement("button");&nbsp; &nbsp; &nbsp; &nbsp; cb.id = "deletecomment_" + task.id + "_" + commentindex;&nbsp; &nbsp; &nbsp; &nbsp; cb.innerHTML = "Delete comment";&nbsp; &nbsp; &nbsp; &nbsp; cb.onclick = function() {deleteComment(task.id, commentindex);};&nbsp; &nbsp; &nbsp; &nbsp; c.appendChild(cb);&nbsp; &nbsp; &nbsp; &nbsp; l.appendChild(c);&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; &nbsp; let b = document.createElement("button");&nbsp; &nbsp; b.id = "addcomment_" + task.id;&nbsp; &nbsp; b.onclick = function() {addComment(task.id);};&nbsp; &nbsp; b.innerHTML = "Add comment";&nbsp; &nbsp; d.appendChild(b);&nbsp; &nbsp; d.appendChild(l);&nbsp; &nbsp; container.appendChild(d);&nbsp; })}function addComment(taskid) {&nbsp; let newcomment = prompt("Enter comment");&nbsp; if (newcomment) {&nbsp; &nbsp; let tasklist = getTasksList();&nbsp; &nbsp; let filtered = tasklist.filter(task => task.id == taskid);&nbsp; &nbsp; if (filtered[0]) {&nbsp; &nbsp; &nbsp; let thisTask = filtered[0];&nbsp; &nbsp; &nbsp; thisTask.comments.push(newcomment);&nbsp; &nbsp; &nbsp; let thisIndex = taskList.findIndex((task) => task.id == taskid);&nbsp; &nbsp; &nbsp; taskList[thisIndex] = thisTask;&nbsp; &nbsp; }&nbsp; &nbsp; saveTasksList(taskList);&nbsp; &nbsp; displayTasks();&nbsp; }}function addNewTask() {&nbsp; let newTask = prompt("Enter task description");&nbsp; let taskList = getTasksList();&nbsp; let lastindex = localStorage.getItem("tasksindex");&nbsp; let index = getNewIndex();&nbsp; let a = {"id": index, "task": newTask, "comments": []}&nbsp; taskList.push(a);&nbsp; saveTasksList(taskList);&nbsp; displayTasks();}function deleteComment(taskid, commentindex) {&nbsp; let tasklist = getTasksList();&nbsp; let filtered = tasklist.filter(task => task.id == taskid);&nbsp; // as long as there is at least one task with the taskid value, find and delete the comment&nbsp; // based on the index position of the comment in the comments array&nbsp; if (filtered[0]) {&nbsp; &nbsp; let thisTask = filtered[0];&nbsp; &nbsp; thisTask.comments.splice(commentindex, 1);&nbsp; &nbsp; let thisIndex = taskList.findIndex((task) => task.id == taskid);&nbsp; &nbsp; taskList[thisIndex] = thisTask;&nbsp; }&nbsp; saveTasksList(taskList);&nbsp; displayTasks();}function getTasksList() {&nbsp; let tasks = localStorage.getItem("tasks");&nbsp; taskList = JSON.parse(tasks);&nbsp; if (!taskList) {&nbsp; &nbsp; taskList = [];&nbsp; }&nbsp; return taskList;}function saveTasksList(taskList) {&nbsp; localStorage.setItem("tasks", JSON.stringify(taskList));}function getNewIndex() {&nbsp; let lastindex = localStorage.getItem("tasksindex");&nbsp; let idx = 0;&nbsp; if (!lastindex) {&nbsp; &nbsp; idx = 1;&nbsp; } else {&nbsp; &nbsp; idx = Number(lastindex) + 1;&nbsp; }&nbsp; localStorage.setItem("tasksindex", idx);&nbsp; return idx;}function removeAll() {&nbsp; localStorage.removeItem("tasks");&nbsp; localStorage.removeItem("tasksindex");&nbsp; displayTasks();}window.onload = checkTasks;</script><style type="text/css">.commentdiv {&nbsp; border:1px solid black;&nbsp; width:1000px;&nbsp; padding:5px;&nbsp; border-radius:5px;}button {&nbsp; margin-left:10px;}h3 {&nbsp; width:100%;&nbsp; border-bottom: 1px dotted black;}ul {&nbsp; list-style-type:decimal;}</style></head><body><h2>My task list <button id="addNewTaskButton" onclick="addNewTask();">Add new task</button></h2><hr><div id="tasks"></div><button id="removeAll" onclick="removeAll();">Remove all tasks</button></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript