【实验室】localStorage实现本地存储
HTML代码如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JS Bin</title> </head> <body> <h1>web stroage</h1> <input type="text" id='input'> <p id="msg"></p> <button type="button" value="保存数据" onclick="saveStorage('input');">保存数据</button> <button type="button" value="显示数据" onclick="loadStorage('msg');">显示数据</button> </body> </html>
Js代码如下:
function saveStorage(x){ var _this=document.getElementById(x); var _value=_this.value; localStorage.setItem('key',_value); } function loadStorage(y){ var msg= document.getElementById('msg'); var key = localStorage.getItem('key'); msg.innerHTML= key; }
显示效果如下: