我有一個使用localStorage和javascript的代碼。我試圖添加更多的插槽,如slot1,slot2,slot3最多5.我只是複製並粘貼,然後更改變量名稱,如slot1,slot2,slot3到5,但它不會工作。非常感謝幫助。添加或清除localStorage上的按鈕點擊並顯示在html中的值
的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/
你'reduceSlot'功能似乎缺少一個參數。你用'reduceSlot(1)'調用它。 – Thilo
但它不會工作,當我添加'reduceSlot(1)' – newbie
你能幫助我嗎? @Thilo – newbie