【发布时间】:2018-10-27 10:47:02
【问题描述】:
我正在尝试使用 HTML5 本地存储。我填写输入字段并单击“存储”按钮。然后我重新加载页面并清除输入字段。然后我单击“检索”按钮,期望从本地存储中填充 P1 输入字段。但该字段仍为空白。我的错误是什么?
<button onclick="store()" >STORE</button>
<button onclick="retrieve()" >RETRIEVE</button> <br />
<input type = "text" id = "P1"><br />
<script>
function store()
{
localStorage.setItem("S1", "P1");
}
</script>
<script>
function retrieve()
{
document.getElementById("P1").value= localStorage.getItem("S1");
}
</script>
【问题讨论】:
-
也许
localStorage.getItem("S");? -
我修好了。还是不行。
-
您当前的代码在 localStorage 中保存了一个字符串文字,也许您的意思是
document.getElementById("P1").innerHTML吗?另外,请注意,如果您使用file协议(例如 Microsoft Edge)加载页面,某些浏览器不提供 localStorage。 -
呃,没有。 document.getElementById("P1").innerHTML 也是一个字面量。
-
你的代码有效
标签: html local-storage