我有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