我正在使用javascript和html制作游戏。我想创建一个高分功能。 x
是您当前得分的名称。在下面的代码中,header
表示“高分”,但在完成游戏后应该说出一个数字。Javascript Highscore Not Working
<h2 id="highscore">Highscore</h2>
function highscorefunction() {
var highscore = localStorage.getItem("highscore");
if(highscore !== null){
if (x > highscore) {
localStorage.setItem("highscore", x);
document.getElementById("highscore").textContent = highscore ;
}
}else{
localStorage.setItem("highscore", x);
document.getElementById("highscore").textContent = highscore ;
}
}
注意:下面的代码表示[对象HTMLHeadingElement]而不是“高分”
function highscorefunction() {
var highscore = localStorage.getItem("highscore");
if(highscore !== null){
if (x > highscore) {
localStorage.setItem("highscore", x);
}
}else{
localStorage.setItem("highscore", x);
}
}
document.getElementById("highscore").textContent = highscore ;
可有人请告诉我我哪里错了
凡'x'意味着神奇来自:
所以它不使用HTML元素,以便定义
highscore
之外的功能? –*“x是您当前得分的名称。”*,请告诉我们您是如何获得该价值的。确保它是你的想法,而不是HTML元素。 –