代码应该做的是输入用户输入并将其存储在LocalStorage中,然后获取已存储的输入并显示它。由于是localStorage,因此应保留输入并在关闭和打开后显示它。
我一直关注this tutorial,并尝试修改并适应我的意愿。 问题在于它无法保存。
抱歉,这是一个不好的问题
function saveBio() {
var newBio = document.getElementById("bio").value; //get user input
localStorage.setItem("storeBio", newBio); //store it
displayBio(newBio) //take to next function
}
function displayBio(newBio) {
document.getElementById("bio").innerHTML = localStorage.getItem("storeBio");//display the localstorge
}
<input id="bio" type="text" name="bio" placeholder="Bio" onmouseout=saveBio() ">
答案 0 :(得分:1)
像这样固定这一行:
document.getElementById("bio").innerHTML = localStorage.getItem(newBio); // you need to get the element by providing the Id value
document。 getElementById 是一个函数,该函数使用ID值查找匹配的元素并返回它。然后,访问该元素的 innerHTML 属性并进行更改。
答案 1 :(得分:1)
您的代码存在问题,是displayBio()
中的这一行:
document.getElementById.innerHTML = localStorage.getItem("storeBio");
您没有提供getElementById
的ID,因为您要显示简历,因此应该是这样:
document.getElementById("bio").innerHTML = localStorage.getItem("storeBio");
答案 2 :(得分:0)
在getItem函数中,您使用键的值来获取项目。
存储接口的getItem()方法,当传递键名时, 将返回该键的值;如果键不存在,则返回null。
使用 localStorage.getItem(“ storeBio”) ,而不是 localStorage.getItem(newBio) >
除了您必须将您的 getElementById 更改为: document.getElementById(“ bio”) .. innerHTML
答案 3 :(得分:0)
您未正确设置和获取值。
localStorage.setItem
接受2个参数作为输入。 (键和值)
localStorage.getItem
接受1个参数作为检索的键。
使用一个键和一个值设置您的项目,然后使用您指定的键检索它。