Javascript: 无法从 localStorage 中提取
Javascript: Unable to pull from localStorage
为 class 项目制作待办事项列表 - 能够让项目“添加”并附加到列表中,但是当您刷新页面时,项目 returns 作为 undefined
。检查 localStorage.todo
,我看到它提取了我需要的部分值,但尚未使用,但不是实际的 innerText
。我开始添加一些位来检查已完成的项目和一个删除按钮,但它被注释掉了,希望不会影响事情 - 试图隔离错误。
我猜我的问题出在 localStorage.setItem
中,但我不确定会是什么。我试过将 newTodo.innerText
值更改为正在使用的其他变量,但没有 returns。在 returns 下面我输入了一个输入值,但在页面刷新时又丢失了。
const todoForm = document.querySelector('#todoForm');
const todoList = document.querySelector('#todoList');
let todoItem = document.querySelector('#todoItem');
// Pull from storage
const savedList = JSON.parse(localStorage.getItem('todo')) || [];
for (let i = 0; i < savedList.length; i++)
{
let newTodo = document.createElement('li');
newTodo.innerText = savedList[i].task;
todoList.appendChild(newTodo);
}
// Add Item
todoForm.addEventListener('submit', function(e){
e.preventDefault();
let newTodo = document.createElement('li');
let newItem = document.querySelector('#todoItem').value;
newTodo.innerText = newItem;
todoForm.reset();
todoList.appendChild(newTodo);
// Clear Form Field on submit and storage
savedList.push({ task: newTodo.innertext, isCompleted: false });
localStorage.setItem('todo', JSON.stringify(savedList));
});
// Add Remove Button
// let removeItem = document.createElement('button');
// removeItem.innerText = "Remove";
// newTodo.append(removeItem);
// Strike through item or remove item
// todoList.addEventListener('click', function(e){
// if (e.target.tagName === 'BUTTON'){
// e.target.parentElement.remove();
// }
// else if (e.target.tagName === 'LI'){
// e.target.style.textDecoration = 'line-through';
// }
// });
驼峰式错误不是 innertext
而是 innerText
savedList.push({ task: newTodo.innerText, isCompleted: false })
为 class 项目制作待办事项列表 - 能够让项目“添加”并附加到列表中,但是当您刷新页面时,项目 returns 作为 undefined
。检查 localStorage.todo
,我看到它提取了我需要的部分值,但尚未使用,但不是实际的 innerText
。我开始添加一些位来检查已完成的项目和一个删除按钮,但它被注释掉了,希望不会影响事情 - 试图隔离错误。
我猜我的问题出在 localStorage.setItem
中,但我不确定会是什么。我试过将 newTodo.innerText
值更改为正在使用的其他变量,但没有 returns。在 returns 下面我输入了一个输入值,但在页面刷新时又丢失了。
const todoForm = document.querySelector('#todoForm');
const todoList = document.querySelector('#todoList');
let todoItem = document.querySelector('#todoItem');
// Pull from storage
const savedList = JSON.parse(localStorage.getItem('todo')) || [];
for (let i = 0; i < savedList.length; i++)
{
let newTodo = document.createElement('li');
newTodo.innerText = savedList[i].task;
todoList.appendChild(newTodo);
}
// Add Item
todoForm.addEventListener('submit', function(e){
e.preventDefault();
let newTodo = document.createElement('li');
let newItem = document.querySelector('#todoItem').value;
newTodo.innerText = newItem;
todoForm.reset();
todoList.appendChild(newTodo);
// Clear Form Field on submit and storage
savedList.push({ task: newTodo.innertext, isCompleted: false });
localStorage.setItem('todo', JSON.stringify(savedList));
});
// Add Remove Button
// let removeItem = document.createElement('button');
// removeItem.innerText = "Remove";
// newTodo.append(removeItem);
// Strike through item or remove item
// todoList.addEventListener('click', function(e){
// if (e.target.tagName === 'BUTTON'){
// e.target.parentElement.remove();
// }
// else if (e.target.tagName === 'LI'){
// e.target.style.textDecoration = 'line-through';
// }
// });
驼峰式错误不是 innertext
而是 innerText
savedList.push({ task: newTodo.innerText, isCompleted: false })