我无法理解为什么我的代码没有被执行

时间:2017-01-18 20:57:27

标签: javascript html

我下面有三个按钮,标有+1的按钮应该将钱包加一。标记为Transfer money to Bank的按钮应将钱包金额移至银行并将钱包重置为0.但是,没有任何内容被调用或正常工作。我绝对卡住了。请帮忙。

<html>
    <head>
      <title>Area 52</title>
    </head>
    <body>
      <h1><center>Area 52</center></h1>
      <p><center><p Id="name">User</p><p>Is currently logged on</p></center><p>
    <script>
      var money=0
      var bank=0
      var Money_handler(money,bank,op){
        alert("!")
        if (op=="add"){
          money+=1
          document.getElementById('wallet').innerHTML = money;
          return money;
        }
        if (op=="bank"){
          bank=bank+money
          document.getElementById('bank').innerHTML = bank;
          money=0
          document.getElementById('wallet').innerHTML = money;
          return money
        }

      }
      var namer=function(){
        var x=prompt("What is your name?");
        document.getElementById('name').innerHTML = x;
      }
        </script>
    <center>
      <button type="button" onclick=money=Money_handler(money,bank,"add")>+1</button>
      <button type="button" onclick=namer()>Enter username</button>
      <button type="button" onclick=Money_handler(money,bank,"bank")>Transfer money to Bank</button>
      <p Id="wallet">Wallet: $0</p>
      <p Id="bank">Bank: $0</p>
    </center>
    </body>
 </html>

2 个答案:

答案 0 :(得分:0)

您只需使用var而不是函数:

function Money_handler(money,bank,op){

修改 当你更新钱时,你会忘记其余部分:

.innerHTML = "Wallet: $"+money;

您必须使用钱包/银行在所有3个地方更改此内容。

答案 1 :(得分:0)

这应该有效。除了function关键字,您还错过了onclick属性值的引号。

&#13;
&#13;
<html>
<head>
  <title>Area 52</title>
</head>
<body>
  <h1><center>Area 52</center></h1>
  <p><center><p Id="name">User</p><p>Is currently logged on</p></center><p>
<script>
 var money = 0
 var bank = 0
 var Money_handler= function(money, bank, op) {
  alert("!")
  if (op == "add") {
    money += 1
    document.getElementById('wallet').innerHTML = money;
    return money;
  }
  if (op == "bank") {
    bank = bank + money
    document.getElementById('bank').innerHTML = bank;
    money = 0
    document.getElementById('wallet').innerHTML = money;
    return money
  }

 }
 var namer = function() {
  var x = prompt("What is your name?");
  document.getElementById('name').innerHTML = x;
 }
</script>
<center>
  <button type="button" onclick="Money_handler(money,bank,'add');">+1</button>
  <button type="button" onclick="namer()">Enter username</button>
  <button type="button" onclick="Money_handler(money,bank,'bank')">Transfer money to Bank</button>
  <p Id="wallet">Wallet: $0</p>
  <p Id="bank">Bank: $0</p>
</center>
</body>
</html>
&#13;
&#13;
&#13;

注意:我只纠正了语法缺陷。我认为你应该研究逻辑。