localStorage存储方法
localStorage.name = localStorage["name"]=‘vanida‘;localStorage.setItem("name","vanida");//这三种设置值方式是一样的;
localStorage获取值方法
var name = localStorage["name"] = localStorage.name= localStorage.getItem("name");//这三种获取值方式是一样的;
localStorage清值方法
localStorage.removeItem("name");或者localStorage.name=‘‘;//清除name的值
localStorage只能存储字符串,如果需要存储对象,首先要转化为字符串。利用JSON.stringify();
例子:
var person = {name:"vanida","sex":"girl","age":25};localStorage.setItem("person",JSON.stringify(person));
// localStorage.person="{"name":"vanida","sex":"girl","age":25}"
然后取出person的对象你可以用JSON.parse();
例子:
person = JSON.parse(storage.getItem("person"));
// {name: "vanida", sex: "girl", age: 25}
时间: 2024-10-10 02:02:40