简体   繁体   English

存储点击数到本地存储HTML5

[英]Storing clicks count to localstorage html5

Made a http://jsfiddle.net/ddvQU/30/ jsfiddle that counts the clicks in a div area. 制作了一个http://jsfiddle.net/ddvQU/30/ jsfiddle来计算div区域中的点击次数。

 <textarea id="ta" placeholder="Type your text here..."></textarea>
 <p id="ta-log"></p>

I am looking to store that number of clicks so when the user navigates away from the page (Closes it) I can recall the number from localstorage. 我希望存储该点击次数,因此当用户离开页面(关闭页面)时,我可以从本地存储中调出该点击次数。

I've updated your jsfiddle and it does what you want: 我已经更新了您的jsfiddle ,它可以满足您的要求:

$( function() {
    var clickCount = localStorage.getItem('clickCount');
    clickCount = clickCount ? parseInt(clickCount) : 0;
    var $num = $('.num');
    $num.text(clickCount);
    $('.box').click( function() {
        $num.text(++clickCount);
        localStorage.setItem('clickCount', clickCount);
    });
});

I haven't used try ... catch blocks, but in real life you should, as you could get errors trying to access localStorage, because it can be disabled or you could have consumed your disk quota. 我没有使用try ... catch块,但是在现实生活中应该这样做,因为尝试访问localStorage时可能会出错,因为可以禁用它,或者您可能已经消耗了磁盘配额。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM