localstorage.clear()无效

时间:2015-06-15 13:07:10

标签: javascript html5

我正在使用html5 localstorage写一篇文章。 但不知何故,清除按钮不起作用。 这是代码

    <form>
    <input type="text" id="note_text">
    <input type="button" id="add_button" value="Add Sticky Note to Self">
    <input type="button" id="clear_button" value="Clear The Notes">

</form>
<ul id="stickyList">

</ul>

The Js

    function init()
{


    var button = document.getElementById("add_button");
    button.onclick = createSticky;
    for (var i = 0; i < localStorage.length; i++) {
        var key = localStorage.key(i);
        if (key.substring(0, 6) == "sticky") {
            var value = localStorage.getItem(key);
            addStickyToDOM(value);
        }
    }
}


function addStickyToDOM(value) {
    var sticky = document.createElement("li");
    sticky.setAttribute("class", "sticky");
    var t = document.createTextNode(value);
    sticky.appendChild(t);
    document.getElementById("stickyList").appendChild(sticky);

}


function createSticky() {
    var value = document.getElementById("note_text").value;
    var key = "sticky_" + localStorage.length;
    localStorage.setItem(key, value);
    addStickyToDOM(value);
}


function clearStorage() {
    localStorage.clear();
    cleart('cleaded THe Stoegae');
}

var clearBtn=document.getElementById("clear_button");
clearBtn.onclick=clearStorage;


window.onload = init;

这是现场小提琴 FIDDLE

当我点击清除按钮时,它什么也没做。 请告诉我我在哪里做错了。 感谢,

0 个答案:

没有答案