我有JavaScript代码来计算按钮点击次数。但是重新加载页面时,计数器设置为零。Javascript函数计数按钮点击?
是否有任何解决方案来计算按钮点击,并在页面重新加载时保持该变量值?
var count = 0;
var button = document.getElementById("countButton");
var display = document.getElementById("displayCount");
// Line commented out as SO does not allow it
// window.localStorage.setItem("on_load_counter", count);
button.onclick = function() {
count++;
display.innerHTML = count;
}
<input type="button" value="Count" id="countButton" />
<p>The button was pressed <span id="displayCount">0</span> times.</p>
使用[Web存储API(https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API) – Satpal
可以节省柜台在饼干或HTML5存储\ –
利用会话存储或本地存储的那个Satpal是正确的 – Jigar7521