简体   繁体   English

永久收藏夹列表

[英]List of Persistent Favourites

I need to keep track of a list of 25 favourites which are added from a list of ~100 entries. 我需要跟踪从约100个条目中添加的25个收藏夹的列表。 These favourites would be in a list of say 这些收藏夹会在说列表中

  • "favourite 1" 「最爱1」
  • "favourite 2" 「最爱2」
  • "favourite 3" .... so on “收藏夹3” ..依此类推

and I need them to be stored persistently. 我需要将它们永久存储。 I also would require them to be able to be deleted and replaced with another favourite string value. 我还要求它们能够被删除并替换为另一个喜欢的字符串值。 I have looked at How do I store an array in localStorage? 我看过如何将数组存储在localStorage中? but that doesn't work for me because when I declare var names=[]; 但这对我不起作用,因为当我声明var names=[]; on the javascript file, every time my favourites function runs it redeclares the array and clears everything. 在javascript文件上,每当我的收藏夹函数运行时,它都会重新声明数组并清除所有内容。

I have tried doing something like: 我尝试做类似的事情:

function addToFav(string)
{
    if (localStorage.fav1)  // if fav1 was created before
    {
        alert("local storage fav1 present, don't make variables");
    }
    else
    {
        alert('fav variables never made, make them now');
        var i=1;
        for (i=1; i<=25; i++)
        {
            var favNumber = "fav" + i;
            localStorage.favNumber = "x";
        }
        alert(localStorage.fav1);                   // outputs "undefined"
    }
}

it was my intention to make localStorage variables of fav1 , fav2 , fav3 ... fav25 and then manage them individually. 我打算将localStorage变量设置为fav1fav2fav3 ... fav25 ,然后分别进行管理。 But this doesn't work since calling localStorage.favNumber = "x"; 但这不起作用,因为调用localStorage.favNumber = "x"; makes the local storage variable favNumber equal to "x" and not fav+i . 使本地存储变量 favNumber等于"x"而不是fav+i

I'm out of ideas at the moment; 我现在没主意了; I have looked at http://playground.html5rocks.com/#async_transactions to try using a HTML5 database but I'm very new to web development and it seems a bit too much for what I'm trying to do. 我看过http://playground.html5rocks.com/#async_transactions尝试使用HTML5数据库,但是我对Web开发来说是一个新手,对于我想做的事情似乎有点过多。 Does anyone know how to fix this issue? 有人知道如何解决此问题吗? Any information would be helpful. 任何信息都有帮助。

This might help. 这可能会有所帮助。 I use these to store info in Local Storage with cookie backup... 我使用这些信息通过Cookie备份将信息存储在本地存储中...

function setLocalStorage(c_name, value) {
    var exdays = 30;
    // if localStorage is present, use that
    if (('localStorage' in window) && window.localStorage !== null) {
        localStorage[c_name] = value;
    } else {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + exdays);
        var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" +     exdate.toUTCString());
        document.cookie = c_name + "=" + c_value;
    }
}

function getLocalStorage(c_name) {
    // if localStorage is present, use that
    if (('localStorage' in window) && window.localStorage !== null) {
        return localStorage[c_name];
    } else {
        var i, x, y, ARRcookies = document.cookie.split(";");
        for (i = 0; i < ARRcookies.length; i++) {
            x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
            y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
            x = x.replace(/^\s+|\s+$/g, "");
            if (x == c_name) {
                return unescape(y);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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