日期:2014-05-17 浏览次数:20686 次
<!DOCTYPE html> <html> <head> <meta charset = "utf-8" /> <title>LocalStorage </title> <script type="text/javascript" src="jquery-1.7.1.min.js"></script> <script type="text/javascript"> var db = window.localStorage; if(db) { if(window.addEventListener) { window.addEventListener("db",displayStorageEvent); console.log('设置监听'); } } function displayStorageEvent(e) { console.log('进入事件处理'); console.log('改变的字段是'+e.key); console.log('旧的值是'+e.oldValue); console.log('新的值是'+e.newValue); } function setdata() { if(db) { db.setItem('author', $("#author").val()); } } function showStorage() { var s =''; for(var i=0;i<db.length;i++) { s = s + db.key(i)+ " : " + db.getItem(db.key(i)) + "<br>" } $("#data")[0].innerHTML = s; } </script> </head> <body> <input type="button" value="显示存储数据" onclick="showStorage()"/> <form id ="info"> <label for="author">作者:</label> <input type="text" name="author" id="author" /> <br/> <input type="button" value="设置LocalStorage" onclick="setdata()"/> </form> <P id="data"></P> </body> </html>