如何在游戏中向用户显示高分?我无法使用onload()
,那么如何显示存储的高分?这是我获得分数的代码。
var hs = localStorage["hs"];
function myFunction() {
$("#demo").text(hs);
}
答案 0 :(得分:0)
<script type="text/javascript">
localStorage.setItem("highscore","100");
var v=localStorage.getItem("highscore");
alert(v);
</script>
你需要使用setItem()为变量赋值,并使用getItem()来获取值
答案 1 :(得分:0)
这会对你有帮助。
<!DOCTYPE html>
<html>
<head>
<title>A game</title>
</head>
<body>
<div id="showscore"></div>
// Keep the script tag at the end of your HTML code.
<script type="text/javascript">
//localStorage.setItem("highScore","564544");
var highScore = localStorage.getItem("highScore");
// Considering you already have highScore set in local storage.
// Uncomment your line number 10 if you want to set in local storage
if(highScore){
document.getElementById('showscore').innerHTML = highScore;
}
else{
document.getElementById('showscore').innerHTML = "Play your first game";
}
</script>
</body>
</html>
您可以在此处使用innerHTML dom属性来插入存储在本地存储中的高分。
确保在html代码的末尾插入脚本,因为脚本需要首先渲染所有元素。