是一種你不主動清除它,它會一直將存儲數據存儲在客戶端的存儲方式,即便你關閉了客戶端(瀏覽器),屬於本地持久層儲存瀏覽器
用於本地存儲一個會話(session)中的數據,一旦會話關閉,那麼數據會消失,好比刷新。session
localStorage.name ='vanida;spa
localStorage["name"]='vanida';對象
localStorage.setItem("name","vanida");rem
//這三種設置值方式是同樣的;
字符串
localStorage獲取值方法get
var name = localStorage["name"]string
var name= localStorage.nameio
var name= localStorage.getItem("name");class
//這三種獲取值方式是同樣的;
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}