我已经创建了一个待办事项应用程序,并且要将待办事项保存在localStorage
中。
我正在考虑添加localStorage
属性,如下所示:
addForm.addEventListener("submit", (e) => {
e.preventDefault();
let todo = addForm.add.value.trim();
if (todo.length) {
localStorage.setItem(todo);
generateTemplate(todo);
addForm.reset();
}
}
但是,它不起作用。所以,我的问题是,在代码的哪一部分中添加属性是最佳实践,而在哪一部分中最好也添加
getItem
方法?这是没有
localStorage
属性的完整代码:const addForm = document.querySelector(".add");
const list = document.querySelector(".todos");
const search = document.querySelector(".search input");
// generate new toDo's
const generateTemplate = (todo) => {
let html = ` <li class="list-group-item d-flex justify-content-between align-items-center">
<span>${todo}</span><i class="far fa-trash-alt delete"></i>
</li>`;
list.innerHTML += html;
};
// submit the todo
addForm.addEventListener("submit", (e) => {
e.preventDefault();
let todo = addForm.add.value.trim();
if (todo.length) {
generateTemplate(todo);
addForm.reset();
}
});
// delete todo's
list.addEventListener("click", (e) => {
if (e.target.classList.contains("delete")) {
e.target.parentElement.remove();
}
});
// filter the toDo's
const filterTodos = (term) => {
Array.from(list.children)
.filter((todo) => !todo.textContent.toLowerCase().includes(term))
.forEach((todo) => todo.classList.add("filtered"));
Array.from(list.children)
.filter((todo) => todo.textContent.toLowerCase().includes(term))
.forEach((todo) => todo.classList.remove("filtered"));
};
search.addEventListener("keyup", () => {
const term = search.value.trim().toLowerCase();
filterTodos(term);
});
body {
background-color: #352f5b;
}
.container {
max-width: 400px;
}
input[type="text"],
input[type="text"]:focus {
color: #fff;
border: none;
background: rgba(0, 0, 0, 0.2);
max-width: 400px;
}
.todos li {
background: #423a6f;
}
.delete {
cursor: pointer;
}
.filtered {
display: none !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>toDoList</title>
<script src="https://kit.fontawesome.com/fa5117c01c.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous" />
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<header class="text-center text-light my-4">
<h1 class="text-ligth mb.4">thingsToDo</h1>
<form action="" class="search"><input type="text" class="form-control m-auto" name="search" placeholder="Search toDo's"></form>
</header>
<ul class="list-group todos mx-auto text-light"></ul>
<form class="add text-center my-4">
<label class="text-light">Add new toDo...</label>
<input type="text" class="form-control m-auto" name="add">
</form>
</div>
<script src="index.js"></script>
</body>
</html>
最佳答案
Storages对象(与LocalStorage一起使用)允许保存字符串(特定于DOMStrings),因此,如果要保存javascript对象,应首先将其转换为字符串(例如JSON.stringify)
同样,在您的代码上,setItem
的语法也不正确。您需要指定a key that identifies the object
localStorage.setItem('todo', todo);
请注意,这只会保存待办事项。因此,当您获得待办事项时,您将需要执行localStorage.getItem('todo')
,这当然只会返回您保存的最后一个待办事项。一个解决方案可能是为您的待办事项存储空间:
var todos = []
每次创建新的待办事项时,都将该待办事项添加到该对象中todos.push(todo)
并将其保存到本地存储localStorage.setItem('todos', JSON.stringify(todos))
当您返回该列表时,需要将字符串parse返回以获取实际对象:var todosString = localStorage.getItem('todos');
var todos = JSON.parse(todosString);
考虑到这只会使您的待办事项数据进入该变量。然后,您需要重新填充您的dom。这可以通过重用
generateTemplate
函数来完成。例如。
todos.forEach(todo => generateTemplate(todo));
我还建议您查看LocalStorage的MDN页面:https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage以及使用Web存储API页面中的完整示例:https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API