源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>编程字典</title> <script> function clickCounter() { if(typeof(Storage) !== "undefined") { if (sessionStorage.clickcount) { sessionStorage.clickcount = Number(sessionStorage.clickcount)+1; } else { sessionStorage.clickcount = 1; } document.getElementById("result").innerHTML = "你点击了 " + sessionStorage.clickcount + " 次."; } else { document.getElementById("result").innerHTML = "对不起, 你的浏览器不支持 web storage..."; } } </script> </head> <body> <p><button onclick="clickCounter()" type="button">点击我!</button></p> <div id="result"></div> <p>点击按钮看计数器增加.</p> <p>关闭标签或者窗口,再试试,计数器将重置.</p> </body> </html>
运行结果