javascript - I need to add local storage to my to do list and I'm clueless
问题描述
I am working on a to do list for a final project in school. I have the list itself set up and working but I forgot to add local storage so that when the page is refreshed it keeps the values that the user set up, like deleting list items or adding new ones. 1) I don't know where in my code to insert it and 2) I don't know the format, like "this variable that I already used goes here" type thing. I pasted the code to codepen and it can be found here: https://codepen.io/otterspawdesign/pen/bJXaYm this is the javascript:
var listitems = document.getElementsByTagName("li");
var i;
for (i = 0; i < listitems.length; i++) {
var span = document.createElement("span");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
listitems[i].appendChild(span);
}
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
if (ev.target.tagName === 'li') {
ev.target.classList.toggle('checked');
}
}, false);
function newElement() {
var li = document.createElement("li");
var inputValue = document.getElementById("typefield").value;
var t = document.createTextNode(inputValue);
li.appendChild(t);
if (inputValue === '') {
alert("Type an item to add to your list.");
} else {
document.getElementById("thelist").appendChild(li);
}
document.getElementById("typefield").value = "";
var span = document.createElement("SPAN");
var txt = document.createTextNode("\u00D7");
span.className = "close";
span.appendChild(txt);
li.appendChild(span);
for (i = 0; i < close.length; i++) {
close[i].onclick = function() {
var div = this.parentElement;
div.style.display = "none";
}
}
}
I'm on a tight deadline (tonight at midnight) so if someone can explain it to me like I'm 5 years old and provide me with code and tell me how it works, i mean step by step, I will be so so grateful.
解决方案
var Todos = JSON.parse(localStorage.getItem('todos')) || [];
for (var i = 0; i < Todos.length; i++) {
newElementFromLocalStorage(Todos[i])
}
This is the trick. The first line of code needs to go at the top of your javascript. It returns either an array of the todos from your localstorage or an empty array when the localstorage is empty (first load). The for loop will then perform a function on all these todo's.
The only thing you need to do (pun intended) is write the newElementFromLocalStorage
function. A function that takes a string, and creates a new element from it.
Todos.push(newTodo);
localStorage.setItem('todos', JSON.stringify(Todos));
These lines of code need to be included in the newElement
function. The first line pushes a new todo to the todo's array (or the empty array, which will then become the todo's). The second line of code sets the localstorage to the new 'state' with the newly added todo.
In these lines of code the newTodo
variable is not declared, that's something you need to do (oh yes, another one). It should just be the string you read from the input.
I realize this is a schoolproject, but I do want to mention that for an actual application like this you wouldn't use local storage, you would connect a database. Hope this helps! Good luck!
推荐阅读
- azure - 如何根据文件模式将不同的文件加载到不同的表中?
- javascript - 在 onSelect 处从 material-ui 自动完成中检索条目的键,而不是值
- react-native - es6 和 redux 嵌套对象的可变性
- flutter - 用可为空的值颤振 FutureBuilder
- cloud-foundry - Pivotal Cloud Foundry 中的物理文件位置
- javascript - threejs lerp 和相机动画不流畅
- javascript - JavaScript - 为什么 innerHTML 属性在包含 HTML 时显示为文本?
- javascript - 没有数据的 React Native 中不同的、不同的 FlatList 项
- laravel - 邮件程序错误:SMTP 错误:无法连接到 SMTP 主机
- javascript - 这个出口是什么意思?这是一个执行两个功能的对象吗?