我正在用纯HTML / CSS / JS集成赌博功能重新创建一个加密货币水龙头,以更好地了解它们的工作原理以及如何从user
逐步过渡到site
并逐步学习JS。我正在尝试复制三个功能,例如存款,掷骰子以获得免费余额和赌博。
我在存款时遇到了2个问题:
当我浏览3页时,显然会因为重载.js文件而将余额重置为0,我应该如何进行此练习?我应该开始使用Cookie吗?浏览这三个页面时,是否可以存储当前会话的值或实现持久值?
route/deposit.html
上不起作用,它为变量增加了价值,但它并未在网站上编辑当前余额,但在route/deposit.html?#
等不同的URL上却起作用。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>
这里是示例
// 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");