Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript P标记没有得到更新_Javascript_Jquery_Html_Css_Ajax - Fatal编程技术网

Javascript P标记没有得到更新

Javascript P标记没有得到更新,javascript,jquery,html,css,ajax,Javascript,Jquery,Html,Css,Ajax,我正在尝试创建保存在localstorage中的高分。该应用程序是一个测验应用程序,它保存并在最后显示前5名的分数(无论是否完成)。这里的问题是,我在in html文件中创建的p标记不会被我在js文件中创建的高分列表中的值更新。代码如下: <div id = "EndSection"> <h3>The Game is Over. Top 5 Players are:</h3> <div class = "Player"> <p id = "P

我正在尝试创建保存在localstorage中的高分。该应用程序是一个测验应用程序,它保存并在最后显示前5名的分数(无论是否完成)。这里的问题是,我在in 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>

比赛结束了。前五名选手是:
xoxo

30

然后我只需复制带有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();>


提前谢谢

各种小问题正在阻止代码工作:

一,。 您的姓名和分数只是字符串-尝试对其进行JSON.parse应该会导致“意外令牌”异常。你没看到吗?确保您能够看到javascript异常(在Chrome中,按F12并单击“控制台”)

二,。 localStorage.setItem(“SavedHighList”应为localStorage.setItem(“SavedHighListObjectz”)。为避免类似错误,请将密钥另存为变量:

var storageKey="SavedHighListObjectz";
...
localStorage.getItem(storageKey)
...
三,。
你只有一个玩家的p标签?这可能很明显,但是(在你修复了#1和#2之后)你的示例代码将在
document.getElementById(“Player2”)中爆炸.innerText
在为其他4个最高分添加html之前

您可以在JSFIDLE中发布吗?我认为您的问题是属性中的空间应该是这样的:
id=“EndSection”
instand of:
id=“EndSection”
首先删除所有解析代码。它是一个javaScript对象,因此不需要解析它。然后单击
,创建一个我可能希望避免将变量命名为“布尔”的,也是。除其他原因外,它没有描述它正在做什么。我想你需要做一些认真的调试Markus。我将你的代码放入一个小提琴中,默认情况下没有调用任何代码。我调用了2个console.log,但都没有显示。你至少需要让它工作一点:在JavaScript自动类型转换之后,
null==undefined
为真。使用严格相等的测试可以消除所有疑问:
if(localStorage.getItem(“SavedHighListObjectz”)==null)
谢谢@Traktor53您是对的-删除了该项目符号