你可以在这上面申请localStorage吗?如果我点击&#34; Deduct&#34;,id&#34; slot&#34;将减少到1.我想学习的是如何应用localStorage。如果插槽是9,如果我将刷新/关闭浏览器或重新启动计算机并再次运行程序,则插槽将保留为9.我很难学习localStorage,需要帮助主人。< / p>
<script>
var availableSlot1 = 10;
var reduceSlot1 = function(valueToDeduct1){
availableSlot1 = availableSlot1 - valueToDeduct1;
document.getElementById('slot').innerHTML = availableSlot1;
var x = storage.key(availableSlot1);
if (availableSlot1 == 0){
document.getElementById('x').innerHTML = "FULL";
document.getElementById("button1").style.display = "none";
}
};
</script>
<p id="slot">10</p>
<a href="javascript:reduceSlot1(1)" id="button1">Deduct</a>
EDITED:
<script>
var slot = localStorage.getItem("slot");
if (typeof slot == "undefined") {
slot = 10;
}
document.getElementById("slot").innerHTML = slot;
var reduceSlot1 = function reduceSlot(by)
{
if (slot >= by) {
slot -= by;
document.getElementById("slot").innerHTML = slot;
localStorage.setItem("slot", slot);
}
else {
document.getElementById('slot').innerHTML = "FULL";
document.getElementById("button1").style.display = "none";
}
}
</script>
我遵循了代码,但是它没有用。
答案 0 :(得分:1)
您可以使用localStorage.name
使用本地存储,代替名称,您可以使用任何属性。
var i = 1;
localStorage.name = i;
答案 1 :(得分:0)
http://jsfiddle.net/flocsy/mrhwK/
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('x').innerText = "FULL";
document.getElementById("button1").style.display = "none";
}
}
document.getElementById("button1").onclick = reduceSlot;