来自 localstorage 的值未保存在屏幕上
value from localstorage is not getting saved on screen
我在 js 中创建了一个简单的文本框应用程序,当我在应用程序中输入一个值时,它会暂时保存,但我想永久保存它。我使用了本地存储,但我不喜欢它。
这里是JavaScript部分
function addValue(){
let ad = document.getElementById("u");
let newli = document.createElement("li");
newli.textContent = document.getElementById("box3").value;
document.getElementById("box3").value = " ";
let local = ad.appendChild(newli);
localStorage.setItem('name',local.textContent);
}
这里是html部分
<ul id="u">
<li>task1 </li>
<li> task 2 </li>
<li> task 3</li>
</ul>
<input type="text" id= "box3">
<input type="button" value="add " onclick="addValue()">
您需要将数据存储在数组中。
这样保存 localStorage.setItem('name',local.textContent);
会用新值覆盖内容。
使用数组,您可以追加新内容而不是覆盖它。
使用此代码
var storage = localStorage.getItem("name");
if (storage) {
var storageContent = JSON.parse(localStorage.getItem("name"));
storageContent.push(newli.textContent);
localStorage.setItem("name", JSON.stringify(storageContent));
} else {
localStorage.setItem("name", JSON.stringify([newli.textContent]));
}
关于您在评论中提出的问题。
首先我们要调用文档的onload函数。
每当加载文档时都会调用此函数,因此是用于此项目的正确函数。
PS:我假设您熟悉箭头函数,这就是我使用它们的原因。如果您觉得它令人困惑,请告诉我
document.onload = (()=>{
// NB: IN ORDER FOR THIS CODE TO WORK MAKE SURE IT APPEARS AT THE BOTTOM OF THE HTML PAGE
// We get the elements from the localStorage and parse it into an array since it is a string at this stage
const items = JSON.parse(localStorage.getItem("name"))
// We select the parent element we want to append the items to
const parentUlToAppendTo = document.querySelector("#u")
// We use the map functions with template literals i.e (``) to return HTMLElements with the items as their innerHTML
const listItems = () => items.map(item => `<li>${item}</li>`)
// We Append The Elements To The parentUlToAppendTo Element
parentUlToAppendTo.insertAdjacentHTML("afterbegin",listItems())
})()
``
我在 js 中创建了一个简单的文本框应用程序,当我在应用程序中输入一个值时,它会暂时保存,但我想永久保存它。我使用了本地存储,但我不喜欢它。
这里是JavaScript部分
function addValue(){
let ad = document.getElementById("u");
let newli = document.createElement("li");
newli.textContent = document.getElementById("box3").value;
document.getElementById("box3").value = " ";
let local = ad.appendChild(newli);
localStorage.setItem('name',local.textContent);
}
这里是html部分
<ul id="u">
<li>task1 </li>
<li> task 2 </li>
<li> task 3</li>
</ul>
<input type="text" id= "box3">
<input type="button" value="add " onclick="addValue()">
您需要将数据存储在数组中。
这样保存 localStorage.setItem('name',local.textContent);
会用新值覆盖内容。
使用数组,您可以追加新内容而不是覆盖它。
使用此代码
var storage = localStorage.getItem("name");
if (storage) {
var storageContent = JSON.parse(localStorage.getItem("name"));
storageContent.push(newli.textContent);
localStorage.setItem("name", JSON.stringify(storageContent));
} else {
localStorage.setItem("name", JSON.stringify([newli.textContent]));
}
关于您在评论中提出的问题。
首先我们要调用文档的onload函数。 每当加载文档时都会调用此函数,因此是用于此项目的正确函数。
PS:我假设您熟悉箭头函数,这就是我使用它们的原因。如果您觉得它令人困惑,请告诉我
document.onload = (()=>{
// NB: IN ORDER FOR THIS CODE TO WORK MAKE SURE IT APPEARS AT THE BOTTOM OF THE HTML PAGE
// We get the elements from the localStorage and parse it into an array since it is a string at this stage
const items = JSON.parse(localStorage.getItem("name"))
// We select the parent element we want to append the items to
const parentUlToAppendTo = document.querySelector("#u")
// We use the map functions with template literals i.e (``) to return HTMLElements with the items as their innerHTML
const listItems = () => items.map(item => `<li>${item}</li>`)
// We Append The Elements To The parentUlToAppendTo Element
parentUlToAppendTo.insertAdjacentHTML("afterbegin",listItems())
})()
``