Javascript gameover页面-无法保存到本地存储

Javascript gameover页面-无法保存到本地存储,javascript,cordova,local-storage,Javascript,Cordova,Local Storage,我想给我的比赛增加一个高分。现在我有这个: var score = 0; score变量起作用,但每当我得到喜欢的分数,总共60分时,当使用console.loghighscore检查时,它表示高分仍然为零 我用这个来存储高分: localStorage.bestscore=分数; var bestscore=localStorage.bestscore要在localStorage中存储某些内容,必须使用“setItem” localStorage.setItem('bestscore',

我想给我的比赛增加一个高分。现在我有这个:

var score = 0;
score变量起作用,但每当我得到喜欢的分数,总共60分时,当使用console.loghighscore检查时,它表示高分仍然为零

我用这个来存储高分:

localStorage.bestscore=分数;
var bestscore=localStorage.bestscore要在localStorage中存储某些内容,必须使用“setItem”

localStorage.setItem('bestscore', score)
然后使用

localStorage.getItem('bestscore')
有关功能的更多信息:


要在localStorage中存储某些内容,必须使用“setItem”

localStorage.setItem('bestscore', score)
然后使用

localStorage.getItem('bestscore')
有关功能的更多信息:

localStorage有两种方法getItem和setItem。 下面是一个例子:

localStorage.setItem('bestScore', score);
var bestScore = localStorage.getItem('bestScore');
localStorage有两种方法getItem和setItem。 下面是一个例子:

localStorage.setItem('bestScore', score);
var bestScore = localStorage.getItem('bestScore');

下面将把highscore设置到localStorage中,然后在进行评分时,将从本地存储获取highscore,如果新评分高于现有highscore,则将用新值覆盖highscore。希望这有帮助。请注意,这只是代码的一部分-需要将其放入在评分时调用的函数中

var score=0;
localStorage.setItem("high_Score",score);
var highScore = localStorage.getItem("high_Score");
if(score > highScore){localStorage.setItem("high_Score",score)};
然后显示它:

var highScore = localStorage.getItem("high_Score");
document.getElementById("bestscore").innerHTML=highScore;

下面将把highscore设置到localStorage中,然后在进行评分时,将从本地存储获取highscore,如果新评分高于现有highscore,则将用新值覆盖highscore。希望这有帮助。请注意,这只是代码的一部分-需要将其放入在评分时调用的函数中

var score=0;
localStorage.setItem("high_Score",score);
var highScore = localStorage.getItem("high_Score");
if(score > highScore){localStorage.setItem("high_Score",score)};
然后显示它:

var highScore = localStorage.getItem("high_Score");
document.getElementById("bestscore").innerHTML=highScore;

以下是函数:在页面加载-调用set_score初始化值,然后在输入字段中输入带有score id的号码-调用score,它应该可以工作

function set_score(){
    var score=0;
    localStorage.setItem("high_Score",score);
    document.getElementById("bestscore").innerHTML=score;
}

function score(){
    var score=parseInt(document.getElementById("score").value);
    var highScore = localStorage.getItem("high_Score");
    if(score > highScore){localStorage.setItem("high_Score",score)};
    var new_highScore = localStorage.getItem("high_Score");
    document.getElementById("bestscore").innerHTML=new_highScore;
}

在my pooter上按预期工作这里有一些功能:在页面加载-调用set_score来初始化值,然后在输入字段中输入号码,id为score-调用score,它应该工作

function set_score(){
    var score=0;
    localStorage.setItem("high_Score",score);
    document.getElementById("bestscore").innerHTML=score;
}

function score(){
    var score=parseInt(document.getElementById("score").value);
    var highScore = localStorage.getItem("high_Score");
    if(score > highScore){localStorage.setItem("high_Score",score)};
    var new_highScore = localStorage.getItem("high_Score");
    document.getElementById("bestscore").innerHTML=new_highScore;
}

在我的pooter上按预期工作

我刚刚创建此文件以显示它-按预期工作。我将控制台设置为显示本地/会话存储,这样我可以看到数字何时被修改,并且也可以按预期工作

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<body onload="set_score()">
    <p>The highest Score so far is: <span id="bestscore"></span></p>
                <input type="text" id="score" value="" >
                    <button type = "button" onclick="score()">score</button>
<script>
function set_score()
    {
        var score=0;
        localStorage.setItem("high_Score",score);
        document.getElementById("bestscore").innerHTML=score;
    }

function score(){
    var score=parseInt(document.getElementById("score").value);
    var highScore = localStorage.getItem("high_Score");
    if(score > highScore){localStorage.setItem("high_Score",score)};
    var new_highScore = localStorage.getItem("high_Score");
    document.getElementById("bestscore").innerHTML=new_highScore;
    }
</script>
</body> 
</head>
</html>

只需将其复制到notepadd++文档中,将其另存为html并在浏览器中运行即可。希望它能对Gav有所帮助

我刚刚创建了这个文件来显示它-工作正常。我将控制台设置为显示本地/会话存储,这样我可以看到数字何时被修改,并且也可以按预期工作

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<body onload="set_score()">
    <p>The highest Score so far is: <span id="bestscore"></span></p>
                <input type="text" id="score" value="" >
                    <button type = "button" onclick="score()">score</button>
<script>
function set_score()
    {
        var score=0;
        localStorage.setItem("high_Score",score);
        document.getElementById("bestscore").innerHTML=score;
    }

function score(){
    var score=parseInt(document.getElementById("score").value);
    var highScore = localStorage.getItem("high_Score");
    if(score > highScore){localStorage.setItem("high_Score",score)};
    var new_highScore = localStorage.getItem("high_Score");
    document.getElementById("bestscore").innerHTML=new_highScore;
    }
</script>
</body> 
</head>
</html>
只需将其复制到notepadd++文档中,将其另存为html并在浏览器中运行即可。希望它能帮助Gav尝试使用window.localStorage:

我希望有帮助

尝试使用window.localStorage:



我希望有帮助

抱歉-搞砸了最后一行-SHPULL be fine NOW似乎是一个很好的解决方案,但不符合我的本地预览心情它需要一个主机?我只是把一个带有输入和按钮的div放在一起,调用score函数,它按预期工作。score和highscore的初始值为0,然后在输入框中输入一个值并调用函数后-如果新的分数高于现有的highscore,highscore loalstorage值将被修改抱歉-弄糟了最后一行-现在应该可以了似乎是一个很好的解决方案,但不适用于我的本地预览需要一个主机吗?我只是把一个div与一个输入和调用score函数的按钮放在一起,它按预期工作。score和highscore的初始值为0,然后在输入框中输入一个值并调用函数后,如果新分数高于现有的highscore,highscore loalstorage值将被修改。什么不起作用?设置innerHTML时,使用的是“最佳”,而不是“最佳分数”。请参阅@gavgrifSee的最后一个答案:什么不起作用?当您设置innerHTML时,您使用的是“best”,而不是“bestscore”。请参阅@gavgrifSee的最后一个答案:您的代码中有一个打字错误。更改为:localStorage.getItem'bestscore';我将在一秒钟内重新检查它仍然无法正常工作。请提供有关如何以及在何处测试代码的更多详细信息。我使用XAMPP设置本地主机并在那里测试我的代码。你想看一下吗?请将我添加到skype@Drusama2您的代码中有一个打字错误。更改为:localStorage.getItem'bestscore';我将在一秒钟内重新检查它仍然无法正常工作。请提供有关如何以及在何处测试代码的更多详细信息。我使用XAMPP设置本地主机并在那里测试我的代码。你想看一下吗?请将我添加到skype@drusama2I我使用了localhost,但仍然无法工作。。。我们可以通过skype聊天并告诉我它是如何为您工作的吗@drusama2hi-我为您准备了这个-在我的pooter上运行良好-只需复制文档并将其保存为html并在浏览器中运行即可。我将控制台窗口设置为显示本地/会话存储,以便可以查看其中设置的值。我使用过localhost,但仍然没有
工作我们可以通过skype聊天并告诉我它是如何为您工作的吗@drusama2hi-我为您准备了这个-在我的pooter上运行良好-只需复制文档并将其保存为html并在浏览器中运行即可。我将控制台窗口设置为显示本地/会话存储,以便可以查看其中设置的值。