Javascript 每当我刷新浏览器时,即使它已经在本地存储中,插槽也会保持显示10
问题是,当我点击按钮,它减少了插槽,它的罚款。假设剩余的插槽是9,但当我刷新浏览器时,它会返回到10,但当我再次单击时,它已经是8,因为我之前已经扣除了它。我关心的是,刷新浏览器时,插槽始终显示为10。我感谢那些愿意帮助我的人Javascript 每当我刷新浏览器时,即使它已经在本地存储中,插槽也会保持显示10,javascript,Javascript,问题是,当我点击按钮,它减少了插槽,它的罚款。假设剩余的插槽是9,但当我刷新浏览器时,它会返回到10,但当我再次单击时,它已经是8,因为我之前已经扣除了它。我关心的是,刷新浏览器时,插槽始终显示为10。我感谢那些愿意帮助我的人 <script> var slot = localStorage.getItem("slot"); if (slot == null) { slot = 10; } document.getElementBy
<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>
var slot=localStorage.getItem(“slot”);
如果(插槽==null){
槽=10;
}
document.getElementById(“slot”).innerText=slot;
函数reduceSlot(){
如果(插槽>=1){
槽--;
document.getElementById(“slot”).innerText=slot;
setItem(“插槽”,插槽);
}
否则{
document.getElementById('slot').innerText=“FULL”;
document.getElementById(“button1”).style.display=“无”;
}
}
document.getElementById(“button1”).onclick=reduceSlot;
函数clearLocalStorage(){
localStorage.clear();
}
10
清除所有
为什么要用href和onclick调用reduce插槽?那剧本真的在身体上方吗?那是你的问题。您在错误控制台中看到了什么?猜测错误消息。它显示Uncaught TypeError:无法将console error的属性“innerText”设置为null,因为您正在尝试访问document.getElementById(“插槽”)
。需要在元素呈现到页面后调用它。文档准备就绪、页面加载或将脚本移动到元素之后。