作個記錄 jquery 操做sessionjquery
// jquery.session.js 簡單使用方法session
添加數據
$.session.set('key', 'value')spa
刪除數據
$.session.remove('key');code
獲取數據
$.session.get('key');blog
清除數據
$.session.clear();rem
//localStorage存值永久有效 function setItems(){ var user = {}; user.name = 'Adam Li'; user.age = 25; user.home = 'China'; localStorage.setItem('userinfo',JSON.stringify(user)); } //localStorage取值 function getItems(){ var data = JSON.parse(localStorage.getItem('userinfo')); console.log("name:"+data.name+'\r age:'+data.age+"\r home:"+data.home); } //localStorage刪除指定鍵對應的值 function deleteItem(){ localStorage.removeItem('userinfo'); console.log(localStorage.getItem('userinfo')); }
if(window.sessionStorage){ alert('ok'); }else{ alert('fail'); } // 設置值 sessionStorage.setItem('key_a', 1); // 取值 var key_a = sessionStorage.getItem('key_a'); console.log(key_a); // 刪除 sessionStorage.removeItem('key_a'); console.log(sessionStorage.getItem('key_a'));// null sessionStorage.setItem('key_b', 1); sessionStorage.setItem('key_c', 2); // 清除全部鍵值 sessionStorage.clear(); console.log(sessionStorage.key_b); console.log(sessionStorage.key_c);