【发布时间】:2020-07-09 16:23:18
【问题描述】:
我正在使用纯 HTML/CSS/JS 中的集成赌博重新创建一个加密货币水龙头,以便更好地了解它们如何工作以及余额如何从 user 逐步转移到 site 并学习 JS所以我试图复制三个功能,例如存款、掷骰子以获得免费余额和赌博。
我在押金上遇到了两个问题:
-
当我浏览这 3 个页面时,显然余额重置为 0,因为它确实重新加载了 .js 文件,我应该如何进行这个练习?我应该开始使用 cookie 吗?有没有办法在当前会话中存储值或在浏览这 3 个页面时实现持久值?
- 存款表格在
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