我已经开始做游戏,有点类似于“花比尔·盖茨的钱”,但是我说过要对其进行一些修改。 我已经完成了第一项,并且尝试使用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+
答案 0 :(得分:0)
您的script.js就像这样:
var money = 10;
var pen = 1;
function ammountpen() {
var mpen = money-pen;
document.getElementById("money").innerHTML = "Money Left: " + mpen;
money = money-1;
}
function sellpen() {
var ppen = money+pen;
document.getElementById("money").innerHTML = "Money Left: " +ppen;
money = money+1;
}
答案 1 :(得分:-1)
您可以尝试使用此JS代码:
money = 10; //dollars
function ammountpen() {
money--;
document.getElementById("money").innerHTML = "Money Left: " + money;
}
function sellpen() {
money++;
document.getElementById("money").innerHTML = "Money Left: " +money;
}