【发布时间】:2018-11-14 16:28:52
【问题描述】:
我正在尝试在 localStorage 中存储多条数据。但是,只存储了一件,我不知道为什么。这是代码
<!DOCTYPE html>
<html>
<body>
<div id="result"></div>
<div id="result2"></div>
<script>
if (typeof(Storage) !== "undefined") {
// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML =
localStorage.getItem("lastname");
}
if (typeof(Storage) !== "undefined") {
// Store
localStorage.setItem("lastname", "Jones");
// Retrieve
document.getElementById("result2").innerHTML =
localStorage.getItem("lastname");
}
</script>
</body>
</html>
在 Chrome 开发人员工具中,应用程序选项卡下存储了“Jones”,但没有存储“Smith”。我检查了类似的问题,但似乎都没有提供具体的解决方案。
【问题讨论】:
-
是键值存储。如果您为同一个键提供第二个值,它将覆盖前一个值。如果您想存储多个值,则存储为逗号分隔的相同键或完全使用不同的键。
标签: javascript html local-storage