2016-05-01 90 views
-1

我正在嘗試爲一個項目創建一個全局點庫,並且一直在試圖獲得我需要的基本原型。有人可以幫我理解爲什麼這個JavaScript代碼不起作用,我該如何解決它?

我問我如何能節省變量的問題,我得到了一個答案,這是好的,但是,它似乎沒有工作,它可能會更好,如果我只是問一個新的問題,而不是花一大堆時間在一個人的評論中談論。

我的問題是,當我按下按鈕,它不會更新。

代碼:

<!DOCTYPE HTML> 
<html> 
    <head> 
     <title>Javascript variable testing</title> 
    </head> 
    <body> 
     <script type="tex/javascript"> 
var clicks = 500000; 

function onClick() { 
    clicks += 1; 
    localStorage.setItem('clicks', clicks); // update the value 
    document.getElementById("clicks").innerHTML = clicks; 

    localStorage.setItem('clicks', clicks); // set the value to localStorage 
}; 

window.onload = function() { 
    clicks = localStorage.getItem('clicks'); // get the value from localStorage 
}; 
     </script> 
     <button type="button" onclick="onClick()">Click this bit</button> 
      <p>Clicks: <a id="clicks">500000</a> 
      </p> 
    </body> 
</html> 

而且,如果是這樣的變量保持不變,甚至對誰使用不同電腦的人任何人都可以做自己的代碼,這將是非常讚賞。

+0

你必須在腳本錯字更換yoiur腳本標籤類型。另外你在localstorage中設置值的兩倍 –

+0

在window.onload裏面你應該更新點擊數的dom元素 –

+0

哦。我有點新的JavaScript,所以我不知道該怎麼做,請你解釋一下嗎? – Billybobof

回答

1

您在腳本類型中拼寫錯誤。它正確的:<script type="text/javascript">

Reffering您的意見,我會做這樣:

<script type="text/javascript"> 
    var clicks; // variable initialization 

    function onClick() { 
     clicks = +clicks + 1; // + operator casts value to number 
     document.getElementById("clicks").innerHTML = clicks; 
     localStorage.setItem('clicks', clicks); // set the value to localStorage 
    }; 

    window.onload = function() { 
     clicks = localStorage.getItem('clicks') || 500000; // get the value from localStorage, otherwise (if is null) set 500000 clicks 
     document.getElementById("clicks").innerHTML = clicks; 
    }; 
</script> 
+0

乾杯,但它只是在我的號碼的末尾放置'1',而不是將1加1,並且在刷新時不會保持不變。 – Billybobof

+0

'clicks'需要被轉換爲'number'。我更新了我的答案。 –

+0

謝謝!但是,我必須問,不同計算機的價值是否保持不變? – Billybobof

0

你忘了一個T,

<script type="text/javascript"> 

看來以後要工作。

0
  1. 更改tex/javascripttext/javascript
  2. 你必須parseInt()從localStorage的價值(否則它是一個字符串)。
<!DOCTYPE HTML> 
<html> 

    <head> 
    <title>Javascript variable testing</title> 

    <script type="text/javascript"> 
     var clicks; 

     function onClick() { 
     clicks += 1; 
     localStorage.setItem('clicks', clicks); // update the value 
     document.getElementById('clicks').innerHTML = clicks; 
     } 

     window.onload = function() { 
     clicks = parseInt(localStorage.getItem('clicks')) || 500000; // get the value from localStorage and parse it to int 
     document.getElementById('clicks').innerHTML = clicks; 
     }; 

    </script> 
    </head> 

    <body> 
    <button type="button" onclick="onClick()">Click this bit</button> 
    <p>Clicks: <a id="clicks">500000</a> 
    </p> 
    </body> 

</html> 

JSFiddle

+0

我試過了,但呃,只要我按下按鈕......「點擊:5.000001111111111e + 24」 – Billybobof

+0

已更新。現在就試試 –

0

小錯誤代碼,否則一切都很好

<!DOCTYPE HTML> 
<html> 
<head> 
<title>Javascript variable testing</title> 
</head> 
<body> 

<script type="text/javascript"> // Small misspelled 
var clicks = 500000; 

function onClick() { 
    clicks += 1; 
    localStorage.setItem('clicks', clicks); // update the value 
    document.getElementById("clicks").innerHTML = clicks; 

    localStorage.setItem('clicks', clicks); // set the value to localStorage 
} // Removed semicolon you added by mistake 

window.onload = function() { 
    clicks = localStorage.getItem('clicks'); // get the value from localStorage 
}; 
</script> 

<button type="button" onclick="onClick()">Click this bit</button> 
<p>Clicks: <a id="clicks">500000</a></p> 

</body> 
</html> 
2

<script type="text/javascript"> // Small misspelled 
var clicks = 500000; 

function onClick() { 
    clicks += 1; 
    localStorage.setItem('clicks', clicks); // update the value 
    document.getElementById("clicks").innerHTML = clicks; 

    localStorage.setItem('clicks', clicks); // set the value to localStorage 
} // Removed semicolon you added by mistake 

window.onload = function() { 
    clicks = localStorage.getItem('clicks'); // get the value from localStorage 
}; 
</script> 
相關問題