第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

僅在 Vanilla JavaScript 中附加在第一篇文章上的評(píng)論

僅在 Vanilla JavaScript 中附加在第一篇文章上的評(píng)論

精慕HU 2023-04-20 17:11:06
我正在創(chuàng)建一個(gè)狀態(tài)發(fā)布和評(píng)論系統(tǒng)。它是在 Vanilla JavaScript 中實(shí)現(xiàn)的。任何人都可以添加帖子并可以對(duì)帖子發(fā)表評(píng)論。一切正常,但評(píng)論部分僅針對(duì)第一篇文章。刪除評(píng)論和帖子工作正常。我不知道問(wèn)題出在哪里,如果有人能幫助我的話。這是 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 回答

?
天涯盡頭無(wú)女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

當(dāng)您使用如下代碼時(shí):

createComment.innerHTML = commentHTMLValue;

您正在完全替換元素的內(nèi)容。嘗試使用:

createComment.innerHTML += commentHTMLValue;

它將新內(nèi)容附加到現(xiàn)有內(nèi)容的末尾。


查看完整回答
反對(duì) 回復(fù) 2023-04-20
?
紅糖糍粑

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊

我不能在這里做一個(gè)片段,因?yàn)椴辉试S使用 localStorage。將此塊復(fù)制到一個(gè)空白文件中并將其另存為 html 文件,然后在瀏覽器中打開(kāi)它。


這就是我認(rèn)為您描述需求的方式,也是基于我評(píng)論中的數(shù)據(jù)格式。它不漂亮,需要大量修飾,但它可以運(yùn)行。


<!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>



查看完整回答
反對(duì) 回復(fù) 2023-04-20
  • 2 回答
  • 0 關(guān)注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)