-2

我目前正在开发一个 HTML5 项目,我需要使用 localstorage。我希望能够从我的本地存储中动态删除项目。这意味着如果我有五个项目,并且我想删除第二个项目,第三个,第四个和第五个项目将自动分配到较低的位置,这样我就不会留下空隙(因为这很烦人我想打印我的整个本地存储)。

4

3 回答 3

3

HTML5 localStorage aka Web Storage,使用键来存储字符串值。因此,您可以通过在存储之前将它们转换为 JSON 来存储对象或数组:

// define an array of objects
var arr = [
    { name : 'first', description : 'first description' },
    { name : 'second', description : 'second description' },
    { name : 'third', description : 'third description' }
];

// store the array to localStorage as a JSON string
localStorage.setItem("data", JSON.stringify(arr));

// retrieve the JSON from localStorage and parse it back to an array
var arr = JSON.parse(localStorage.getItem("data"));

// remove the second object from the array
arr.splice(1, 1);

// let's update the first object too
arr[0].name = 'first name';

// save it back to localStorage
localStorage.setItem("data", JSON.stringify(arr));
于 2013-10-21T09:12:47.120 回答
0
if (localStorage.text) 
{ 
    var text1 = localStorage.text; 
    var splitText1 = text1.split(','); 
    if (splitText1.length > 0) 
    for (i in splitText1) 
    { 
        var div = document.createElement("div"); 
        div.setAttribute("id", "medicament"); 
        div.innerHTML = "<h1>" + splitText1[i] + "</h1>"; document.body.appendChild(div); 
    } 
}

这是我们用来在屏幕上打印内容的代码。

于 2013-10-21T09:16:12.947 回答
0

您可以使用jstorage来解决您的问题。

于 2013-10-21T09:29:42.393 回答