简体   繁体   中英

My localStorage.clear(); won't work

I am trying to make a simple program with localStorage . I created a button to reset all the data in the localStorage . Unfortunately, it won't work I don't know why.

Here's my code:

<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>

I followed this code in a website but it doesn't run. Most of the websites that I browsed were confusing. I need help.

<button onclick="window.localStorage.clear();">Clear All</button>

这应该工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM