我試圖創建一個保存在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