【问题标题】:Learn to use localstorage in JS [duplicate]学习在 JS 中使用 localstorage [重复]
【发布时间】:2021-05-24 03:23:45
【问题描述】:

我想保存并显示一个名为“bestScore”的值

我在互联网上看过我可以看到与我的情况相去甚远的例子

我想知道如何存储和显示一个值

如果有人有一个非常简单易行的例子

谢谢

【问题讨论】:

  • 你试过在网上查到这个吗?有很多关于 localStorage 的教程,很容易找到。
  • localStorage.getItem()localStorage.setItem()。我怀疑您找不到示例来展示LocalStorage 的绝对基础知识
  • 在链接的重复参考中查看this answer

标签: javascript local-storage


【解决方案1】:
//store the value
localStorage.setItem("bestScore", 'here put the score');

//get the value
localStorage.getItem("bestScore");

//you can write it as a function
function saveScore(score){
 localStorage.setItem("bestScore", score);
}

function getScore(scoreName){
 localStorage.getItem(scoreName);
}

//then you can call the function and pass arguments for example
let scoreElement = document.getElementById('score').innerText
saveScore(scoreElement) //call the save function and pass the score to save

scoreElement = getScore('bestScore') //call the get score function and pass the name of the item in the storage

【讨论】:

    猜你喜欢
    • 2011-10-24
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多