1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>本地存储</title> 6 </head> 7 <body> 8 <h1>webStorage本地存储</h1> 9 <p id="msg"></p> 10 <p id="forever"></p> 11 <input type="text" id="input"> 12 <input type="button" value="保存数据" onclick="saveStorage(‘input‘);"> 13 <input type="button" value="读取数据" onclick="loadStorage(‘msg‘);"> 14 </body> 15 <script type="text/javascript"> 16 function saveStorage(id){ 17 var target = document.getElementById(id); 18 var str = target.value; 19 sessionStorage.setItem(‘mess‘,str);//临时保存 20 localStorage.setItem(‘mess‘,str);//永久保存 21 } 22 23 function loadStorage(id){ 24 var target = document.getElementById(id); 25 var forever = document.getElementById(‘forever‘); 26 var msg = sessionStorage.getItem(‘mess‘); 27 target.innerHTML = "临时数据:" + msg; 28 forever.innerHTML = "永久数据:" + localStorage.getItem(‘mess‘); 29 } 30 </script> 31 </html>
原文:http://www.cnblogs.com/loewe0202/p/5698054.html