每次用户向上或向下点击时,都尝试将此值保存到localstorage ???
<script>
var Clicks = 800;
function UpClick() {
Clicks = Clicks + 25;
document.getElementById('CountedClicks').innerHTML = Clicks + '';
if (Clicks > 950) {
Clicks = 950;
}
}
function DownClick() {
Clicks = Clicks - 25;
document.getElementById('CountedClicks').innerHTML = Clicks + '';
if (Clicks <= 0) {
Clicks = 25;
}
}
</script>
<span id="CountedClicks">800</span>
<input type="image" src="http://findicons.com/files/icons/2332/super_m/64/navigation_up_button.png" name="1462_EXPOSURE_TI_UP" value="1462_EXPOSURE_TI_UP_BUTTON_WAS_CLICKED" onclick="UpClick()" />
<input type="image" src="http://findicons.com/files/icons/2332/super_mono/64/navigation_down_button.png" name="1462_EXPOSURE_TI_DOWN" value="" onclick="DownClick()" />
把它变成了一个jsfiddle:http://jsfiddle.net/JRxcr/6/
似乎无法获得getItem或setItem的内容,已经阅读了几小时的材料,但希望有人可以提供帮助
感谢...
答案 0 :(得分:0)
<script>
function UpClick() {
var count = JSON.parse( localStorage.count )
Clicks = count + 25;
document.getElementById('CountedClicks').innerHTML = Clicks;
if (Clicks > 950) {
Clicks = 950;
}
localStorage.count = JSON.stringify( Clicks );
}
function DownClick() {
var count = JSON.parse( localStorage.count )
Clicks = Clicks - 25;
document.getElementById('CountedClicks').innerHTML = Clicks;
if (Clicks <= 0) {
Clicks = 25;
}
localStorage.count = JSON.stringify( Clicks );
}
</script>
<span id="CountedClicks">800</span>
<input type=
"image" src="http://findicons.com/files/icons/2332/super_mono/64/navigation_up_button.png" name="1462_EXPOSURE_TI_UP" value=
"1462_EXPOSURE_TI_UP_BUTTON_WAS_CLICKED" onclick="UpClick()" />
<input type="image" src="http://findicons.com/files/icons/2332/super_mono/64/navigation_down_button.png" name="1462_EXPOSURE_TI_DOWN" value="" onclick="DownClick()" />
答案 1 :(得分:0)
这是一个快速的:
localStorage['StoredClicks'] = Clicks
这会将现有的Clicks
变量保存到名为localStorage
的密钥下的StoredClicks
。
然后,要读回这个值,你可以这样:
var Clicks = localStorage['StoredClicks']
..希望这有帮助!
编辑:如Kopax Jack Herrauer所述,localStorage
将所有数据序列化为字符串,因此在读回整数值时,应确保将其解析为整数。我的回读&#39;上面的例子应该是:
var Clicks = parseInt(localStorage['StoredClicks'], 10)
答案 2 :(得分:0)
这些是我在做一些实验时想出来的。
window.localStorage.setItem(&#39;点击&#39;,20);
window.localStorage.getItem(&#39;点击&#39);
所以一切都会像......
<script>
var Clicks = 800;
function UpClick() {
Clicks = Clicks + 25;
document.getElementById('CountedClicks').innerHTML = Clicks + '';
document
if (Clicks > 950) {
Clicks = 950;
}
}
function DownClick() {
Clicks = Clicks - 25;
document.getElementById('CountedClicks').innerHTML = Clicks + '';
if (Clicks <= 0) {
Clicks = 25;
}
}
function SaveClicks(){
window.localStorage.setItem('clicks',Clicks);
}
function GetClicks(){
return window.localStorage.getItem('clicks');
}
</script>
<span id="CountedClicks">800</span>
<input type=
"image" src="http://findicons.com/files/icons/2332/super_mono/64/navigation_up_button.png" name="1462_EXPOSURE_TI_UP" value=
"1462_EXPOSURE_TI_UP_BUTTON_WAS_CLICKED" onclick="UpClick();SaveClicks();" />
<input type="image" src="http://findicons.com/files/icons/2332/super_mono/64/navigation_down_button.png" name="1462_EXPOSURE_TI_DOWN" value="" onclick="DownClick();SaveClicks();" />