英文:
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 :
<h1>Titre</h1>
<h3>Sous-titre</h3>
<input type="search" id="new-task" placeholder="Ajouter une tâche..." />
<button onclick="répondre()"> 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 />
JAVASCRIPT part :
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 répondre() {
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 = "";
}
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实体代码(如"
)翻译为相应的引号。
英文:
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 "memtasks" doesn't exist in
//localStorage
var tasks = JSON.parse(localStorage.getItem("memtasks")) || [];
//I refactored the code for creating a task in an "addTaskToDOM" function
//because it's used when you open the web page and when you add a new task.
function addTaskToDOM(task) {
var ul = document.getElementById("liste");
var li = document.createElement("li");
//When a task is added to the DOM, you can create a "delete" button that
//will use the "deleteTask" function when clicked
var deleteButton = document.createElement("button");
deleteButton.textContent = "delete";
deleteButton.addEventListener("click",
e => deleteTask(e.target.closest("li"))
);
li.appendChild(task);
li.appendChild(deleteButton);
ul.appendChild(li);
}
//"deleteTask" 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("#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]));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论