9
var cookieValue = document.getElementById("demo");      
var value = cookieValue .getAttribute('value');

if(typeof(Storage)!=="undefined")
{
  alert(value);
  localStorage.setItem = ("GetData" , value);
  alert(localStorage.setItem);
}

function loading()
{
  alert("coming");
  var allcookies = localStorage.getItem('GetData');
  alert(allcookies);
}   

以上是我设置的方式localStorage.setItem,我正在获取 LocalStorage。但是在我没有得到输出的地方,它显示为 Null。请建议我任何解决方案。

4

3 回答 3

17

您的代码应该是:

var cookieValue = document.getElementById("demo");      
    var value = cookieValue .getAttribute('value');
    if(typeof(Storage)!=="undefined")
    {
        alert(value);
        localStorage.setItem("GetData" , value);
        alert(localStorage.getItem("GetData"));

    }

function loading()
{
        alert("coming");
        var allcookies = localStorage.getItem('GetData');
        alert(allcookies);

}

或者

var cookieValue = document.getElementById("demo");      
    var value = cookieValue .getAttribute('value');
    if(typeof(Storage)!=="undefined")
    {
        alert(value);
        localStorage["GetData"] = value;
        alert(localStorage["GetData"]);

    }

function loading()
{
        alert("coming");
        var allcookies = localStorage["GetData"];
        alert(allcookies);

}
于 2012-06-28T14:52:01.860 回答
3

的正确语法setItem

localStorage.setItem("GetData", 值)

不是

localStorage.setItem = ("GetData", 值)

于 2012-06-28T14:51:18.323 回答
1

试试这个

if( window.localStorage ) {
    alert(value);
    localStorage.setItem("GetData" , value);
}
于 2012-06-28T14:52:17.397 回答