我正在尝试创建保存在本地存储中的高分。该应用程序是一个测验应用程序,它在最后保存并显示前 5 名分数(无论是否完成)。这里的问题是我在 html 文件中创建的 p 标签不会被更新我在 js 文件中创建的高分列表中的值。这是代码:
<div id = "EndSection">
<h3>The Game is Over. Top 5 Players are:</h3>
<div class = "Player">
<p id = "Player1">xoxo</p>
<p id = "Score1">30</p>
</div>
然后我只复制带有 2 个 p 标签的 div 类播放器,直到我有 5 个播放器。
javascript代码:
< function SaveToDataBase (){
//Checks if they completed the quizz, and check if there time is
//enough to be top5, if its enough, then it will run newEntry
var boolean = false;
var Trigger = "score";
HighScoreList.forEach(function(entry){
if(entry.hasOwnProperty(Trigger)){
var value = entry[Trigger];
/*you look in your scores, and his total time is less than theres, then he is
added to the list*/
if(thetotaltimer < value){
boolean = true;
}
}
});
if(boolean){
newEntry();
}
}
function newEntry(){
//inserts on index
HighScoreList.splice(5, 0, {name: currentPlayer, score: thetotaltimer });
//sort scores
HighScoreList.sort(function(a,b){
return a.score - b.score;
});
//starts at index 5, then takes away the index right after it (number 6).
HighScoreList.splice(5,1);
localStorage.setItem("SavedHighListObjectz", JSON.stringify(HighScoreList));
document.getElementById("Player1").innerText = JSON.parse(HighScoreList[0].name);
document.getElementById("Score1").innerText = JSON.parse(HighScoreList[0].score);
document.getElementById("Player2").innerText = JSON.parse(HighScoreList[1].name);
document.getElementById("Score2").innerText = JSON.parse(HighScoreList[1].score);
document.getElementById("Player3").innerText = JSON.parse(HighScoreList[2].name);
document.getElementById("Score3").innerText = JSON.parse(HighScoreList[2].score);
document.getElementById("Player4").innerText = JSON.parse(HighScoreList[3].name);
document.getElementById("Score4").innerText = JSON.parse(HighScoreList[3].score);
document.getElementById("Player5").innerText = JSON.parse(HighScoreList[4].name);
document.getElementById("Score5").innerText = JSON.parse(HighScoreList[4].score);
}
function init(){
//this if-statement runs if there is no highscore list already saved
if(localStorage.getItem("SavedHighListObjectz") == undefined) {
HighScoreList = [
{'name': "Sam", 'score': 300000},
{'name': "Markus", 'score': 554000},
{'name': "Cody", 'score': 2210000},
{'name': "David", 'score': 39000000},
{'name': "Jackson", 'score': 55500000}
];
localStorage.setItem("SavedHighList", JSON.stringify(HighScoreList));
}else{
//or else we load this
HighScoreList = JSON.parse(localStorage.getItem("SavedHighListObjectz"))
}
}
init();
questions();>
提前致谢。
最佳答案
各种小问题使您的代码无法工作:
1. 您的姓名和分数只是字符串 - 尝试对它们进行 JSON.parse 应该会导致“意外标记”异常。你没看到吗?确保您能够看到 javascript 异常(在 Chrome 中,按 F12 并单击“控制台”)
2。 localStorage.setItem("SavedHighList"应该是 localStorage.setItem("SavedHighListObjectz"。为避免此类错误,请将您的 key 保存为变量:
var storageKey="SavedHighListObjectz";
...
localStorage.getItem(storageKey)
...
3。
你只有一个玩家的 p 标签?这可能是显而易见的,但是(在你修复了#1 和#2 之后)你的示例代码将在 document.getElementById("Player2").innerText
处爆炸,直到你为其他 4 个添加 html最高分
关于javascript - P 标签没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41419127/