Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 本地存储几乎不需要帮助_Javascript_Html_Local Storage - Fatal编程技术网

Javascript 本地存储几乎不需要帮助

Javascript 本地存储几乎不需要帮助,javascript,html,local-storage,Javascript,Html,Local Storage,我一直试图理解localstorage,但遇到了一个小问题。 当我添加以下代码时,该数字将为零。我刷新站点时,直到按下按钮为止。但我希望在刷新站点后,它能显示最新的号码 function CollectCoin() { if(typeof(Storage) !== "undefined") { if (localStorage.clickcount) { localStorage.clickcount = Number(localStorage.c

我一直试图理解
localstorage
,但遇到了一个小问题。 当我添加以下代码时,该数字将为零。我刷新站点时,直到按下按钮为止。但我希望在刷新站点后,它能显示最新的号码

function CollectCoin() {
    if(typeof(Storage) !== "undefined") {
        if (localStorage.clickcount) {
            localStorage.clickcount = Number(localStorage.clickcount)+1;
        } else {
            localStorage.clickcount = 1;
        }
        document.getElementById("result").innerHTML = "Total coins: " + localStorage.clickcount;
    } else {
        document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
    }
}

我希望有人能帮助我解决这个问题,并向我解释如何解决这个问题。

请查看这里的工作:


您希望它显示数字。这比简单的函数更复杂

HTML:


你的功能对我有用。等等!?他在问什么?他不想通过点击按钮来显示,对吗?每次我刷新页面时,“奇怪”以0硬币开始:(@Stanley1943这是我获取代码的地方!但我希望数字随着按钮的增加而增加,但也希望在刷新时显示我留下的位置,这样人们就不会感到困惑。你的代码对我有效。你确定你没有在另一个piec中重置
localStorage的值。单击count
代码的e?用这个它甚至不会增加更多。谢谢你的回复tho:)用Fiddle检查修改后的答案谢谢你这么多,这就是我要找的:)
function CollectCoin() {
    if(localStorage) {
        if(localStorage.clickCount == "NaN"){
          localStorage.clickCount = 1;
        }
        else{
          localStorage.clickCount++;
        }
       document.getElementById("result").innerHTML = "Total coins: " + localStorage.clickCount;
    }
    else {
        document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
    }
}
<div id="result">Total coins: 0</div>
<button id="coinButton">Collect Coins</button>
//Check if Local Storage is available
if (typeof(Storage) == 'undefined'){
  document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
}else{

  display(); //display the coin count
  //when user click the button, it adds 1
  document.getElementById('coinButton').addEventListener('click', addcoin);
}

function addcoin() {
  localStorage.clickcount = Number(localStorage.clickcount)+1;
  display();
}

//display function
function display() {

  //Check if clickcount is available. Only Number is tested, so use isNaN
  if (!localStorage.clickcount || isNaN(localStorage.clickcount)){
    localStorage.clickcount = 0;
  }
  document.getElementById("result").innerHTML = "Total coins: " + Number(localStorage.clickcount);
}