我有一个使用 localStorage 和 javascript 的代码。我尝试添加更多插槽,例如 slot1、slot2、slot3 最多 5 个。我只是复制并粘贴,然后将变量名称(例如 slot1、slot2、slot3)更改为 5 个。但它不起作用。帮助将不胜感激。
Javascript:
var slot = localStorage.getItem("slot");
if (slot == null) {
slot = 10;
}
document.getElementById("slot").innerText = slot;
function reduceSlot() {
if (slot >= 1) {
slot--;
localStorage.setItem("slot", slot);
if (slot > 0) {
document.getElementById('slot').innerText = slot;
} else {
document.getElementById('slot').innerText = "FULL";
document.getElementById("button1").style.display = "none";
}
}
}
document.getElementById("button1").onclick = reduceSlot;
function clearLocalStorage() {
localStorage.clear();
}
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/barmar/K8stQ/3/