【问题标题】:How should I permanently store the value of a JS variable on an HTML document?我应该如何将 JS 变量的值永久存储在 HTML 文档中?
【发布时间】:2020-07-09 16:23:18
【问题描述】:

我正在使用纯 HTML/CSS/JS 中的集成赌博重新创建一个加密货币水龙头,以便更好地了解它们如何工作以及余额如何从 user 逐步转移到 site 并学习 JS所以我试图复制三个功能,例如存款、掷骰子以获得免费余额和赌博。

我在押金上遇到了两个问题:

  1. 当我浏览这 3 个页面时,显然余额重置为 0,因为它确实重新加载了 .js 文件,我应该如何进行这个练习?我应该开始使用 cookie 吗?有没有办法在当前会话中存储值或在浏览这 3 个页面时实现持久值?

    1. 存款表格在route/deposit.html 上不起作用,它为变量添加了值,但它不是在网站上编辑当前余额,而是在不同的网址(如route/deposit.html?#)上起作用。

let myBalance=0;
let dAmount=0;

function deposit(){

    dAmount = Number(document.getElementById("dAmount").value);
    myBalance = myBalance + dAmount;
    document.getElementById("myBalance").innerHTML=myBalance;
}
/* Style the header with a grey background and some padding */
.header {
    overflow: hidden;
    background-color: #f1f1f1;
    padding: 20px 10px;
  }
  
  /* Style the header links */
  .header a {
    float: left;
    color: black;
    text-align: center;
    padding: 12px;
    text-decoration: none;
    font-size: 18px;
    line-height: 25px;
    border-radius: 4px;
  }
  
  /* Style the logo link (notice that we set the same value of line-height and font-size to prevent the header to increase when the font gets bigger */
  .header a.logo {
    font-size: 25px;
    font-weight: bold;
  }
  
  /* Change the background color on mouse-over */
  .header a:hover {
    background-color: #ddd;
    color: black;
  }
  
  /* Style the active/current link*/
  .header a.active {
    background-color: dodgerblue;
    color: white;
  }
  
  /* Float the link section to the right */
  .header-right {
    float: right;
  }
  
  /* Add media queries for responsiveness - when the screen is 500px wide or less, stack the links on top of each other */
  @media screen and (max-width: 500px) {
    .header a {
      float: none;
      display: block;
      text-align: left;
    }
    .header-right {
      float: none;
    }
  }
   /* Create a static faucet balance indicator */
  .faucetbalance{
      position: absolute;
      bottom: 5%;
  }
<html>
<head>
    <title>Faucet emulation</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<script src="./test.js"></script>
<body>
    <div class="header">
        <a href="#default" class="logo">SupaFaucet</a>
        <div class="header-right">
          <a href="./freeroll.html">Free BITS</a>
          <a href="#hilo">HI-LO</a>
          <a href="#deposit" class="active">Deposit</a>
          <a>Balance: <span id="myBalance">0</span> BITS</a>
        </div>
        <div class="faucetbalance">
            <a>Faucet Balance: <span>0</span> BITS</a>
        </div>
    </div>
    <h1>Deposit BITS</h1>
    <form onSubmit="deposit()">
        <label for="deposit">Deposit:</label><input id="dAmount" type="number">
        <input type="submit">
    </form>    
    
</body>
</html>

【问题讨论】:

  • 你可以使用sessionStorage
  • 您可以使用localStorage,保存值和日期创建。第一个始终是未更改的。
  • @palaѕн 我将阅读如何实现 sessionStorage 并更改“let myBalance”以直接从 HTML 页面获取 0 的值,而不是在 .JS 文档上设置它,这应该可以解决问题。如果会话仍然打开,它应该从 0 开始,然后获取最后一个值。顺便谢谢你的好推! :)
  • @Raphael,这意味着必须重置这些值,对吧?我现在选择sessionStorage,因为它似乎更适合娱乐。还是谢谢你!

标签: javascript html forms browser onsubmit


【解决方案1】:

您应该使用 localStorage 或 sessionStorage。

这里是例子

// Store
  localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");

将数据保存到 localStorage 的语法:

localStorage.setItem("key", "value");

从 localStorage 读取数据的语法:

var lastname = localStorage.getItem("key");

从 localStorage 中删除数据的语法:

localStorage.removeItem("key");

【讨论】:

    猜你喜欢
    • 2011-01-04
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 2021-03-23
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多