源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>编程字典</title> <script> function clickCounter() { if(typeof(Storage) !== "undefined") { if (localStorage.clickcount) { localStorage.clickcount = Number(localStorage.clickcount)+1; } else { localStorage.clickcount = 1; } document.getElementById("result").innerHTML = "你点了 " + localStorage.clickcount + "次."; } else { document.getElementById("result").innerHTML = "对不起,你的浏览器不支持本地web存储"; } } </script> </head> <body> <p> <button onclick="clickCounter()" type="button">点击我!</button> </p> <div id="result"></div> <p>点击按钮看计数器增加.</p> <p>关闭浏览器标签或者窗口,再试试,计数器将继续计数。</p> </body> </html>
运行结果