javaScript--6 sessionStorage与localStorage 设置与获取
<!DOCTYPE html> <html> <head> <title>本地存储</title> <script> function setSessionStorage() { //添加sessionStorage var name = document.getElementById("setSessionStorage").value; var value = document.getElementById("sessionStorageParam").value; window.sessionStorage.setItem(name, value); } function getSessionStorage() { //获取sessionStorage var name = document.getElementById("getSessionStorage").value; var value = window.sessionStorage.getItem(name); if (value != null) { document.getElementById("sessionStorageValue").innerText = value; } else { document.getElementById("sessionStorageValue").innerText = name + "暂无数据"; } } function setlocalStorage() { //添加sessionStorage var name = document.getElementById("setlocalStorage").value; var value = document.getElementById("localStorageParam").value; window.localStorage.setItem(name, value); } function getlocalStorage() { //获取sessionStorage var name = document.getElementById("getlocalStorage").value; var value = window.localStorage.getItem(name); if (value != null) { document.getElementById("localStorageValue").innerText = value; } else { document.getElementById("localStorageValue").innerText = name + "暂无数据"; } } </script> </head> <body> <h1>sessionStorage</h1> 设置SessionStorage:<br /> 名称:<input type="text" id="setSessionStorage" /> 参数:<input type="text" id="sessionStorageParam" /> <input type="button" value="添加sessionStorage" onclick="setSessionStorage()" /><br /> 获取SessionStorage:<br /> 名称:<input type="text" id="getSessionStorage" /><span id="sessionStorageValue"></span> <br /> <input type="button" value="获取sessionStorage" onclick="getSessionStorage()" /> <h1>localStorage</h1> 设置localStorage:<br /> 名称:<input type="text" id="setlocalStorage" /> 参数:<input type="text" id="localStorageParam" /> <input type="button" value="添加localStorage" onclick="setlocalStorage()" /><br /> 获取localStorage:<br /> 名称:<input type="text" id="getlocalStorage" /><span id="localStorageValue"></span> <br /> <input type="button" value="获取localStorage" onclick="getlocalStorage()" /> </body> </html>
创作不易,转摘请标明出处。如果有意一起探讨测试相关技能可加博主QQ 771268289 博主微信:ding17121598
本文来自博客园,作者:怪圣卡杰,转载请注明原文链接:https://www.cnblogs.com/dwdw/p/16750562.html