<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <textarea style="width: 200px;height: 200px" id="mytxt"></textarea> <button id="mybtn">保存</button> <script> var mytxt; var mybtn; window.onload=function () { mytxt=document.getElementById("mytxt"); if(localStorage.text){ //不为空时 mytxt.value=localStorage.text; } mybtn=document.getElementById("mybtn"); mybtn.onclick=function(){ //alert(mytxt.value); localStorage.text=mytxt.value; } } </script></body></html>3、sessionStorage
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <span id="txt">0</span> <button id="mybtn">Add</button> <script> var myspan; var mybtn; var num=0; window.onload=function () { myspan=document.getElementById("txt"); mybtn=document.getElementById("mybtn"); if(sessionStorage.num){ num=sessionStorage.num; showNum(); }else{ num=0; } mybtn.onclick=function () { num++; sessionStorage.num=num; showNum(); } } function showNum(){ myspan.innerHTML=num; } </script></body></html>
新闻热点
疑难解答