亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

僅在 Vanilla JavaScript 中附加在第一篇文章上的評論

僅在 Vanilla JavaScript 中附加在第一篇文章上的評論

精慕HU 2023-04-20 17:11:06
我正在創建一個狀態發布和評論系統。它是在 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>
查看完整描述

2 回答

?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

當您使用如下代碼時:

createComment.innerHTML = commentHTMLValue;

您正在完全替換元素的內容。嘗試使用:

createComment.innerHTML += commentHTMLValue;

它將新內容附加到現有內容的末尾。


查看完整回答
反對 回復 2023-04-20
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

我不能在這里做一個片段,因為不允許使用 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() {

  let tasksList = getTasksList();

  if (tasksList.length == 0) {

    let newTask = prompt("Please enter a task description");

    if (newTask) {

      let thisIndex = getNewIndex();

      let a = {"id": thisIndex, "task": newTask, "comments": []}

      taskList.push(a);

      saveTasksList(taskList);

    }

  }

  displayTasks();

}


function displayTasks() {

  let container = document.getElementById("tasks");

  container.innerHTML = "";

  let taskList = getTasksList();

  taskList.forEach(function(task){

    let d = document.createElement("div");

    d.id = "task_" + task.id;

    d.className = "commentdiv";

    d.innerHTML = "<h3>" + task.task + "</h3>";

    let l = document.createElement("ul");

    l.id = "comments_" + task.id;

    let comments = task.comments;

    if (comments.length > 0) {

      let commentindex = 0;

      comments.forEach(function(comment) {

        let c = document.createElement("li");

        c.innerHTML = comment;

        let cb = document.createElement("button");

        cb.id = "deletecomment_" + task.id + "_" + commentindex;

        cb.innerHTML = "Delete comment";

        cb.onclick = function() {deleteComment(task.id, commentindex);};

        c.appendChild(cb);

        l.appendChild(c);

      })

    }

    let b = document.createElement("button");

    b.id = "addcomment_" + task.id;

    b.onclick = function() {addComment(task.id);};

    b.innerHTML = "Add comment";

    d.appendChild(b);

    d.appendChild(l);

    container.appendChild(d);

  })

}


function addComment(taskid) {

  let newcomment = prompt("Enter comment");

  if (newcomment) {

    let tasklist = getTasksList();

    let filtered = tasklist.filter(task => task.id == taskid);

    if (filtered[0]) {

      let thisTask = filtered[0];

      thisTask.comments.push(newcomment);

      let thisIndex = taskList.findIndex((task) => task.id == taskid);

      taskList[thisIndex] = thisTask;

    }

    saveTasksList(taskList);

    displayTasks();

  }

}


function addNewTask() {

  let newTask = prompt("Enter task description");

  let taskList = getTasksList();

  let lastindex = localStorage.getItem("tasksindex");

  let index = getNewIndex();

  let a = {"id": index, "task": newTask, "comments": []}

  taskList.push(a);

  saveTasksList(taskList);

  displayTasks();

}


function deleteComment(taskid, commentindex) {

  let tasklist = getTasksList();

  let filtered = tasklist.filter(task => task.id == taskid);

  // as long as there is at least one task with the taskid value, find and delete the comment

  // based on the index position of the comment in the comments array

  if (filtered[0]) {

    let thisTask = filtered[0];

    thisTask.comments.splice(commentindex, 1);

    let thisIndex = taskList.findIndex((task) => task.id == taskid);

    taskList[thisIndex] = thisTask;

  }

  saveTasksList(taskList);

  displayTasks();


}


function getTasksList() {

  let tasks = localStorage.getItem("tasks");

  taskList = JSON.parse(tasks);

  if (!taskList) {

    taskList = [];

  }

  return taskList;

}


function saveTasksList(taskList) {

  localStorage.setItem("tasks", JSON.stringify(taskList));

}


function getNewIndex() {

  let lastindex = localStorage.getItem("tasksindex");

  let idx = 0;

  if (!lastindex) {

    idx = 1;

  } else {

    idx = Number(lastindex) + 1;

  }

  localStorage.setItem("tasksindex", idx);

  return idx;

}


function removeAll() {

  localStorage.removeItem("tasks");

  localStorage.removeItem("tasksindex");

  displayTasks();


}


window.onload = checkTasks;



</script>


<style type="text/css">


.commentdiv {

  border:1px solid black;

  width:1000px;

  padding:5px;

  border-radius:5px;

}


button {

  margin-left:10px;

}


h3 {

  width:100%;

  border-bottom: 1px dotted black;

}


ul {

  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>



查看完整回答
反對 回復 2023-04-20
  • 2 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號