Javascript 本地存储待办事项列表
我正在尝试将待办事项列表保存到本地存储中,但这不会发生。我不知道我遗漏了什么。Javascript 本地存储待办事项列表,javascript,local-storage,Javascript,Local Storage,我正在尝试将待办事项列表保存到本地存储中,但这不会发生。我不知道我遗漏了什么。 我的目标是: 1-当我添加待办事项时,它将被保存 2-当我删除任务时,它将从本地存储中删除 //The icon add classes const fas = document.querySelector(".fas"); const ul = document.querySelector("ul"); const input = document.querySelector
我的目标是:
1-当我添加待办事项时,它将被保存
2-当我删除任务时,它将从本地存储中删除
//The icon add classes
const fas = document.querySelector(".fas");
const ul = document.querySelector("ul");
const input = document.querySelector("input");
/*
LocalStorage trying to implement the number id
*/
let taskNumber = 0;
let myTask = new Object();
const singleTask = "Task";
/**
* End Local Storage
*/
//Grab the input
fas.addEventListener("click", () => {
const li = document.createElement("li");
const icon = document.createElement("i");
const anotherIcon = document.createElement("i");
anotherIcon.classList = "fas fa-check";
icon.classList = "fas fa-times";
const inputValue = input.value;
li.innerHTML = inputValue;
li.appendChild(icon);
li.appendChild(anotherIcon);
icon.addEventListener("click", () => {
li.parentElement.removeChild(li);
});
anotherIcon.addEventListener("click", () => {
li.style.background = "#013a6b";
li.style.color = "#fff";
li.style.textDecoration = "line-through";
});
if (inputValue == "") {
return;
}
ul.appendChild(li);
input.value = "";
/**
* Local Storage
*/
let myStorage = window.localStorage;
myTask.task = ++taskNumber + " " + inputValue;
myStorage.setItem(singleTask, JSON.stringify(myTask.task));
console.log(myStorage);
});
您是否使用纯js或任何库?因为有几个库可以改变本地存储的行为。不,只有纯JavaScript,没有任何库。因为这
myTask.task=++taskNumber++inputValue
是一个字符串,我不认为您需要JSON.stringify(myTask.task)
只需myStorage.setItem(singleTask,myTask.task)
通常JSON。stringify
用于将复杂的JSON对象转换为字符串。非常感谢。请参见此处: