我正在进行JavaScript倒计时时钟扩展,我希望用户输入日期,并保存该日期,但每当用户再次打开扩展时,它都不会保存以前的输入。有没有办法永久保存用户输入?提前谢谢!
var countDownDate = new Date(prompt("Enter Your Date")).getTime();
答案 0 :(得分:0)
我只是通过w3schools的例子与你联系,你可以阅读localStorage
api
https://www.w3schools.com/html/html5_webstorage.asp
<!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 = "You have clicked the button " + localStorage.clickcount + " time(s).";
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
}
}
</script>
</head>
<body>
<p><button onclick="clickCounter()" type="button">Click me!</button></p>
<div id="result"></div>
<p>Click the button to see the counter increase.</p>
<p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p>
</body>
</html>