是一種你不主動清除它,它會一直將存儲數據存儲在客戶端的存儲方式,即便你關閉了客戶端(瀏覽器),屬於本地持久層儲存html
用於本地存儲一個會話(session)中的數據,一旦會話關閉,那麼數據會消失,好比刷新。瀏覽器
localStorage.name ='vanida;session
localStorage["name"]='vanida';spa
localStorage.setItem("name","vanida");htm
//這三種設置值方式是同樣的;對象
localStorage獲取值方法blog
var name = localStorage["name"]rem
var name= localStorage.name字符串
var name= localStorage.getItem("name");get
//這三種獲取值方式是同樣的;
localStorage清除特定值方法
//清除name的值
localStorage.removeItem("name");
localStorage.name='';
localStorage清除全部值方法
localStorage.clear()
var person = {name:"vanida","sex":"girl","age":25};
localStorage.setItem("person",JSON.stringify(person));
// localStorage.person="{"name":"vanida","sex":"girl","age":25}"
注意:JSON.stringify()中不要忘了「i」,stringify而不是stringfy!
person = JSON.parse(localStorage.getItem("person"));
// {name: "vanida", sex: "girl", age: 25}