如何获取本地存储值

问题描述 投票:8回答:3
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。请建议我任何解决方案。

javascript html5 local-storage
3个回答
14
投票

你的代码应该是:

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);

}

3
投票

setItem的正确语法是

localStorage.setItem(“GetData”,value)

localStorage.setItem =(“GetData”,value)


0
投票

试试这个

if( window.localStorage ) {
    alert(value);
    localStorage.setItem("GetData" , value);
}
© www.soinside.com 2019 - 2024. All rights reserved.