3种写入存储的方式
if(!window.localStorage){
alert("浏览器支持localstorage");
return false;
}else{
var storage=window.localStorage;
//写入a字段 storage["a"]=1;
//写入b字段 storage.a=1;
//写入c字段 storage.setItem("c",3);
console.log(typeof storage["a"]);
console.log(typeof storage["b"]);
console.log(typeof storage["c"]); }
3种读取方式
if(!window.localStorage){
alert("浏览器支持localstorage");
}else{
var storage=window.localStorage;
//写入a字段 storage["a"]=1;
//写入b字段 storage.a=1;
//写入c字段 storage.setItem("c",3);
console.log(typeof storage["a"]);
console.log(typeof storage["b"]);
console.log(typeof storage["c"]);
//第一种方法读取 var a=storage.a;
console.log(a);
//第二种方法读取 var b=storage["b"];
console.log(b);
//第三种方法读取 var c=storage.getItem("c");
console.log(c); }
网友评论