gpt4 book ai didi

javascript - 我的 localStorage.clear();行不通

转载 作者:行者123 更新时间:2023-11-28 01:55:54 24 4
gpt4 key购买 nike

我正在尝试使用localStorage制作一个简单的程序。我创建了一个按钮来重置 localStorage 中的所有数据。不幸的是,它不起作用,我不知道为什么。

这是我的代码:

<script>
var slot = localStorage.getItem("slot");

if (slot == null) {
slot = 10;
}

document.getElementById("slot").innerText = slot;

function reduceSlot() {
if (slot >= 1) {
slot--;
document.getElementById("slot").innerText = slot;
localStorage.setItem("slot", slot);
}
else {
document.getElementById('slot').innerText = "FULL";
document.getElementById("button1").style.display = "none";
}
}

document.getElementById("button1").onclick = reduceSlot;

function clearLocalStorage(){
localStorage.clear();
}
</script>

<body>
<p id="slot">10</p>
<a href="javascript:reduceSlot(1)" id="button1">Deduct</a>
<button onclick="clearLocalStorage()">Clear All</button>
</body>

我在网站中遵循了此代码,但它无法运行。我浏览的大多数网站都令人困惑。我需要帮助。

最佳答案

<button onclick="window.localStorage.clear();">Clear All</button>

这应该有效。

关于javascript - 我的 localStorage.clear();行不通,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19137137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com