标签:
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 <script src="appWeb.js"></script> 7 </head> 8 <body> 9 <p id="msg"></p> 10 <textarea id="memo" cols="60" rows="10"></textarea><br/> 11 <input type="button" value="追加数据" onclick="saveStorage(‘memo‘)"> 12 <input type="button" value="删除数据" onclick="clearStorage()"> 13 </body> 14 </html>
1 /** 2 * Created by Eagles on 2015/9/5. 3 */ 4 5 function saveStorage(id) { 6 var data = document.getElementById(id).value; 7 var time = new Date().getTime(); 8 // 保存数据的方法 key value 9 localStorage.setItem(time, data); 10 alert("数据已储存"); 11 loadStorage("msg"); 12 loadStorage("msg"); 13 } 14 15 16 function loadStorage(id) { 17 var result = "<table border = ‘2‘>"; 18 for(var i=0; i<localStorage.length;i++){ 19 var key = localStorage.key(i); 20 var value = localStorage.getItem(key); 21 var date = new Date(); 22 date.setTime(key); 23 result += "<tr><td>"+value+"</td><td>"+date+"</td></tr>"; 24 } 25 result += "</table>"; 26 var target = document.getElementById(id); 27 target.innerHTML = result; 28 } 29 30 function clearStorage(){ 31 localStorage.clear(); 32 alert("数据已经删除"); 33 loadStorage("msg"); 34 }
标签:
原文地址:http://www.cnblogs.com/tianyuxieshen/p/4784109.html