我已经开始做一个游戏,有点类似于“花比尔盖茨的钱”,但我说过要稍微改变一下。我做了第一项,我试着用JS编辑它,所以每次你按“买”,它会从你身上移除1美元,或者,当你按“卖”给你1美元。问题是,每次我点击,它只算一次。另外,如果我按“买进”,然后按“卖出”,它会立即给我11美元,而不是10美元。文件名:index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width = device-width, initial-scale = 1.0">
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<title>Spend you own money</title>
<script src="script.js">
</script>
<link rel="stylesheet" href="style.css">
<body>
<noscript>You need to enable javascript in order to have fun!</noscript>
<h1 class="center" id = "money">Money Left: 10</h1><!--Money = 100 000 000 000-->
<div class= "main">
<div class="pen">
<h2>Pen</h2>
<input type="submit" class="sell" value="Sell" onclick="sellpen()">
<!-- <input type="text" class="ammount" value = 0 id = "ammountpen" readonly="readonly" size="9%"> -->
<input type="submit" class="buy" value="Buy" onclick="ammountpen()" >
</div>
</div>
</body>
</head>
</html>
文件名:script.js
var money = 10; //dollars
var pen = 1; // dollar
var mpen = money-pen;
var ppen = money+pen;
function ammountpen() {
document.getElementById("money").innerHTML = "Money Left: " + mpen;
money = money-1;
}
function sellpen() {
document.getElementById("money").innerHTML = "Money Left: " +ppen;
money = money+
您可以尝试使用以下JS代码:
money = 10; //dollars
function ammountpen() {
money--;
document.getElementById("money").innerHTML = "Money Left: " + money;
}
function sellpen() {
money++;
document.getElementById("money").innerHTML = "Money Left: " +money;
}