最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delete from localStorage - Stack Overflow

programmeradmin4浏览0评论

I am trying to use local storage for my little table where i just want to save few things here is my current code:

    $("#dodaj").click(function()
    {


        var str = "<tr onclick='Oznaci(this)'><td>"+stevilka+"</td><td>"+naslov+"</td><td>"+vrsta+"</td><td>"+nujnost+"</td><td>"+datum+"</td></tr>";
        $("#tabela").append(str);

        localStorage.podatki = localStorage.podatki + str;
    });

    $("#odstrani").click(function()
    {
        {
            $(".rdeca").remove();   
            localStorage.removeItem(".rdeca");      
        }
    });




function Oznaci(vrstica)
{
    $(vrstica).css({"background-color":"#FF0000"});
    $(vrstica).attr("class","rdeca");
}

I sucessfully add to the localStorage, but when i call localStorage.removeItem(".rdeca"); it does not remove it. I am probably removing the wrong thing (.rdeca). As i should probably remove the string bined like str from it, but i dont know how i can manage to do thath. THanks for your help

I am trying to use local storage for my little table where i just want to save few things here is my current code:

    $("#dodaj").click(function()
    {


        var str = "<tr onclick='Oznaci(this)'><td>"+stevilka+"</td><td>"+naslov+"</td><td>"+vrsta+"</td><td>"+nujnost+"</td><td>"+datum+"</td></tr>";
        $("#tabela").append(str);

        localStorage.podatki = localStorage.podatki + str;
    });

    $("#odstrani").click(function()
    {
        {
            $(".rdeca").remove();   
            localStorage.removeItem(".rdeca");      
        }
    });




function Oznaci(vrstica)
{
    $(vrstica).css({"background-color":"#FF0000"});
    $(vrstica).attr("class","rdeca");
}

I sucessfully add to the localStorage, but when i call localStorage.removeItem(".rdeca"); it does not remove it. I am probably removing the wrong thing (.rdeca). As i should probably remove the string bined like str from it, but i dont know how i can manage to do thath. THanks for your help

Share Improve this question edited Jan 30, 2013 at 17:05 HyperX asked Jan 30, 2013 at 16:12 HyperXHyperX 391 silver badge6 bronze badges 4
  • 1 I think you need to use localStorage.removeItem("rdeca") – Arun P Johny Commented Jan 30, 2013 at 16:15
  • Nope it does not work too. – HyperX Commented Jan 30, 2013 at 16:16
  • Where are you setting the .rdeca item? I only see you setting the podatki item. – apsillers Commented Jan 30, 2013 at 16:17
  • I mark the clicked lines in table with red color, and this lines should be removed from the localStorage when i click delete. – HyperX Commented Jan 30, 2013 at 16:19
Add a ment  | 

2 Answers 2

Reset to default 8

The only assignment to localStorage that's shown in your code is this:

localStorage.podatki = localStorage.podatki + str;

The name of the item you stored is podatki so you need to remove it using that name:

localStorage.removeItem("podatki")

If your goal is to remove just a part of the value stored under that name, then you'll need to perform that operation manually. That is, you'll need to read the property, modify it as needed, and then write back to it.

The item in localstorage has no jQuery methods to update it.

You need to manually pull the stuff out of localstorage, delete the node, and reinsert it.

发布评论

评论列表(0)

  1. 暂无评论