编辑你的代码:
<!DOCTYPE html> <html> <head> <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 storage..."; } } </script> </head> <body> <p><button onclick="clickCounter()" type="button">点击!</button></p> <div id="result"></div> <p>点击按钮查看递增的数字.</p> <p>关闭浏览器或页签,再试一次,查看数字是递增还是重置的.</p> </body> </html>
查看结果:
phpxuexi.net
- 亲自试一试