删除清单上的按钮

huangapple go评论72阅读模式
英文:

Delete button on a checklist

问题

我正在尝试在“待办事项列表”部分的所有文本旁边添加一个删除按钮,以便它们与文本居中对齐。

以下是你目前没有删除按钮的代码:

HTML部分:

<h1>标题</h1>
<h3>副标题</h3>
<input type="search" id="new-task" placeholder="Add a task..." />
<button onclick="respond()">OK</button>

<hr />
<div id="todo-list">
  <h2>Todo list</h2>
  <ul id="liste">
  </ul>
</div>

<div id="date">
  <input type="datetime-local" value="2023-03-29T11:30" disabled="true" min="2018-06-07T00:00" max="2023-06-14T00:00">
  <div></div>
</div>

JavaScript部分:

var tasks = JSON.parse(localStorage.getItem("memtasks"));

for (let i = 0; i < tasks.length; i++) {
  var t = tasks[i];
  var ul = document.getElementById("liste");
  var li = document.createElement("li");
  li.appendChild(document.createTextNode(t));
  ul.appendChild(li);
}

function respond() {
  console.log("test", tasks);
  var task = document.getElementById("new-task").value;
  if (!task) {
    return;
  }

  tasks.push(task);
  console.log(tasks);
  localStorage.setItem("memtasks", JSON.stringify(tasks));

  var ul = document.getElementById("liste");
  var li = document.createElement("li");
  li.appendChild(document.createTextNode(task));
  ul.appendChild(li);
  document.getElementById("new-task").value = "";
}

希望对你有帮助。

英文:

I am trying to add a delete button next to all of my text in the part "todo list, so they have to be centered with it.

Here is my code I did for now without the delete button :

HTML part :

&lt;h1&gt;Titre&lt;/h1&gt;
&lt;h3&gt;Sous-titre&lt;/h3&gt;
&lt;input type=&quot;search&quot; id=&quot;new-task&quot; placeholder=&quot;Ajouter une t&#226;che...&quot; /&gt;
&lt;button onclick=&quot;r&#233;pondre()&quot;&gt; OK &lt;/button&gt;

&lt;hr /&gt;
&lt;div id=&quot;todo-list&quot;&gt;
  &lt;h2&gt;Todo list&lt;/h2&gt;
  &lt;ul id=&quot;liste&quot;
  &lt;/ul&gt;
&lt;/div&gt;

&lt;div id=&quot;date&quot;&gt;
  &lt;input type=&quot;datetime-local&quot; value=&quot;2023-03-29T11:30&quot; disabled=&quot;true&quot; min=&quot;2018-06-07T00:00&quot; max=&quot;2023-06-14T00:00&quot;&gt;
  &lt;div /&gt;

JAVASCRIPT part :

var tasks = JSON.parse(localStorage.getItem(&quot;memtasks&quot;));

for (let i = 0; i &lt; tasks.length; i++) {
  var t = tasks[i];
  var ul = document.getElementById(&quot;liste&quot;);
  var li = document.createElement(&quot;li&quot;);
  li.appendChild(document.createTextNode(t));
  ul.appendChild(li);
}

function r&#233;pondre() {
  console.log(&quot;test&quot;, tasks);
  var task = document.getElementById(&quot;new-task&quot;).value;
  if (!task) {
    return;
  }

  tasks.push(task);
  console.log(tasks);
  localStorage.setItem(&quot;memtasks&quot;, JSON.stringify(tasks));

  var ul = document.getElementById(&quot;liste&quot;);
  var li = document.createElement(&quot;li&quot;);
  li.appendChild(document.createTextNode(task));
  ul.appendChild(li);
  document.getElementById(&quot;new-task&quot;).value = &quot;&quot;;
}

thanks

答案1

得分: 1

以下是翻译后的代码部分:

// 在这里是一种实现方式。我在代码中添加了注释以进行解释。
      
      // 我添加了一个回退到空数组的选项,以防“memtasks”在
      // localStorage中不存在
      var tasks = JSON.parse(localStorage.getItem("memtasks")) || [];
    
      // 我重构了创建任务的代码,将其放在一个“addTaskToDOM”函数中,
      // 因为它在打开网页时和添加新任务时都会使用。
      function addTaskToDOM(task) {
        var ul = document.getElementById("liste");
        var li = document.createElement("li");

        // 当将任务添加到DOM时,您可以创建一个“删除”按钮,
        // 在单击时使用“deleteTask”函数
        var deleteButton = document.createElement("button");
        deleteButton.textContent = "删除";
        deleteButton.addEventListener("click",
          e => deleteTask(e.target.closest("li"))
        );
      
        li.appendChild(task);
        li.appendChild(deleteButton);
        ul.appendChild(li);
      }
    
      // “deleteTask”将从DOM中删除任务
      // 更新任务数组
      // 然后更新localStorage。
      function deleteTask(task) {
        task.remove();
        tasks = Array.from(document.querySelectorAll("#liste li"))
                  .map(li => li.childNodes[0].textContent);
        localStorage.setItem("memtasks", JSON.stringify(tasks));
      }
    
      function répondre() {
        var task = document.getElementById("new-task").value;
    
        if (!task) {
          return;
        }
    
        tasks.push(task);
        addTaskToDOM(document.createTextNode(task));
        localStorage.setItem("memtasks", JSON.stringify(tasks));
        document.getElementById("new-task").value = "";
      }
    
      for (let i = 0; i < tasks.length; i++) {
        addTaskToDOM(document.createTextNode(tasks[i]));
      }

请注意,我已将HTML实体代码(如&quot;)翻译为相应的引号。

英文:

Here's one way of doing it. I added comments in the code for explanation.

  //I added a fallback to an empty array in case &quot;memtasks&quot; doesn&#39;t exist in
//localStorage
var tasks = JSON.parse(localStorage.getItem(&quot;memtasks&quot;)) || [];
//I refactored the code for creating a task in an &quot;addTaskToDOM&quot; function 
//because it&#39;s used when you open the web page and when you add a new task.
function addTaskToDOM(task) {
var ul = document.getElementById(&quot;liste&quot;);
var li = document.createElement(&quot;li&quot;);
//When a task is added to the DOM, you can create a &quot;delete&quot; button that 
//will use the &quot;deleteTask&quot; function when clicked
var deleteButton = document.createElement(&quot;button&quot;);
deleteButton.textContent = &quot;delete&quot;;
deleteButton.addEventListener(&quot;click&quot;,
e =&gt; deleteTask(e.target.closest(&quot;li&quot;))
);
li.appendChild(task);
li.appendChild(deleteButton);
ul.appendChild(li);
}
//&quot;deleteTask&quot; will remove the task from the DOM 
// update the tasks array 
// then update the localStorage.
function deleteTask(task) {
task.remove();
tasks = Array.from(document.querySelectorAll(&quot;#liste li&quot;))
.map(li =&gt; li.childNodes[0].textContent);
localStorage.setItem(&quot;memtasks&quot;, JSON.stringify(tasks));
}
function r&#233;pondre() {
var task = document.getElementById(&quot;new-task&quot;).value;
if (!task) {
return;
}
tasks.push(task);
addTaskToDOM(document.createTextNode(task));
localStorage.setItem(&quot;memtasks&quot;, JSON.stringify(tasks));
document.getElementById(&quot;new-task&quot;).value = &quot;&quot;;
}
for (let i = 0; i &lt; tasks.length; i++) {
addTaskToDOM(document.createTextNode(tasks[i]));
}

huangapple
  • 本文由 发表于 2023年4月7日 00:47:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定