localStorage 存 取 删
<html><head></head><body> <div id="show"> </div> <button onclick="save()">存值</button> <button onclick="get()">取值</button> <button onclick="remove()">删除</button> <button onclick="clearAll()">清空</button> <p> sessionStorage和localStorage只能存字符串 </p> <script> var show = document.getElementById("show"); var fff = { a: 'aaa', b:'bbb' } function save(){ window.localStorage.setItem("saveData","value123"); console.log("存储成功"); window.localStorage.setItem("saveData2",'{"value":"aaa"}'); window.localStorage.setItem("savePar",JSON.stringify(fff)); window.localStorage.person = JSON.stringify(fff); console.log('all is ok'); } function get(){ var local = window.localStorage.getItem("saveData"); show.innerHTML += local; var local2 = window.localStorage.saveData; show.innerHTML += '---' + local2; show.innerHTML += window.localStorage.saveData2; show.innerHTML += window.localStorage.savePar; console.log(JSON.parse(window.localStorage.saveData2).value,JSON.parse(window.localStorage.savePar),JSON.parse(window.localStorage.person)); } function remove(){ window.localStorage.removeItem("saveData"); } function clearAll(){ localStorage.clear(); } </script> </body></html>