我有一个带有 javascript 和 localStorage 的简单程序。有一个等于 10 的变量槽。当我单击“扣除”时,它会递减。但问题是它在 document.getElementById('slot').innerText = "FULL"; 之前显示 0(零)会触发。我不知道为什么。我想要的是当插槽达到 0 时,它已经不显示零并且显示 FULL。
Javascript:
window.onload = function(){
slot = localStorage.getItem("slot");
if (slot == null) {
slot = 10;
}
document.getElementById("slot").innerText = slot;
document.getElementById("button1").addEventListener('click', function(e){
e.preventDefault();
reduceSlot();
})
}
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";
}
}
HTML:
<p id="slot">10</p>
<a href="javascript:reduceSlot(1)" id="button1">Deduct</a>
<button onclick="window.localStorage.clear();">Clear All</button>
这是链接:http: //jsfiddle.net/K8stQ/