切换按钮内部功能

时间:2015-05-04 19:17:24

标签: javascript html

我正在尝试控制特定按钮的启用/禁用状态。行为应该是:

1。)如果玩家没有足够的“东西”,按钮仍然被禁用;

2。)如果玩家有足够的“东西”,则应启用该按钮。

我已经看到了一些非常复杂的方法解决这个问题,但我正在寻找一些简单易懂的东西。

快速研究显示document.getElementById("myBtn").disabled = true;属性。我试图在index.html文件的按钮中添加一个唯一的id,并在buyGen()函数中使用if / else语句来切换按钮状态,但无济于事。

我的思维方式是正确还是有更简单的方法?我将包含“干净”的代码,以向您展示我所拥有的内容,因为我的添加内容无效。

main.js

var things = 0;
var gen1 = 0;


function thingClick(number) {
    things = things + number;
    document.getElementById("things").innerHTML = things;
}

function buyGen1() {
    var gen1Cost = Math.floor(10 * Math.pow(1.1, gen1));
    if(things >= gen1Cost) {
        gen1 = gen1 + 1;
        things = things - gen1Cost;
        document.getElementById('gen1').innerHTML = gen1;
        document.getElementById('things').innerHTML = things;
    }
    var nextCost = Math.floor(10 * Math.pow(1.1, gen1));
    document.getElementById('gen1Cost').innerHTML = nextCost;
}

window.setInterval(function(){

    thingClick(gen1);

}, 1000);

的index.html

   <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <script type="text/javascript" src="js/main.js"></script>
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
        Things: <span id="things">0</span><br><br>
        <button onClick="thingClick(1)">Get Things</button><br><br>
        <button onClick="buyGen1()">Buy Generator Lvl. 1</button><br><br>
        Generators Lvl. 1: <span id="gen1">0</span> | Cost: <span id="gen1Cost">10</span>

     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

0 个答案:

没有答案