我试图创建一个保存在localStorage的高分。该应用程序是一个quizz应用程序,它可以保存并显示最后5个分数(其完成与否)。这里的问题是,我在html文件中创建的p标签不会被我在js文件中创建的高分列表中的值。这里是代码:p标签没有更新
<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>
,然后我刚才复制的DIV级选手与2 P标签,直到我有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();>
在此先感谢。
您可以发布它的jsfiddle? 我觉得你的问题是在attritube空间的应该是这样的:'ID =“EndSection”的'instand:'ID =“EndSection”' – itzikb
开始通过删除所有的解析代码。它是一个javaScript对象,因此不需要解析它。然后点击''''创建一个[mcve] – mplungjan
我想要避免命名变量“布尔”。除其他原因之外,它没有描述它在做什么。 – seemly