JavaScript 计算器

 

不和你多bb,直接上代码

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Calculator</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
            body{
                font-family: 'Microsoft Yahei';
                background-color: #56351E;
            }
            .calculator{
                width: 300px;
                height: 350px;
                margin: 100px auto;
                background-color: #C47335;
                border-radius: 10px;
            }
            .show{
                height: 80px;
                padding: 10px;
            }
            .show h1{
                margin-top: 0;
                color: #F19953;
                font-size: 20px;
                text-align: center;
            }
            .show input{
                display: block;
                width: 260px;
                height: 24px;
                margin: 0 auto;
                padding-right: 6px;
                text-align: right;
                background-color: #F19953;
                border: none;
            }
            .buttons{
                width: 96%;
                margin: 0 auto;
                text-align: center;

            }
            .buttons button{
                width: 22%;
                margin: 2px 0;
                padding: 12px;
                color: #f19953;
                font-size: 14px;
                background-color: #fff;
                border: none;
                cursor: pointer;
                transition: all .2s;
            }
            button.sum{
                width: 46%;
                padding: 12px;
                margin: -1px;
            }
            .buttons button:hover{
                color: #fff;
                background-color: #f19953;
            }
        </style>
    </head>
    <body>
        <div class="calculator">
            <div class="show">
                <h1>JavaScript Calculator</h1>
                <input id="textbox" type="text" name="" value="" readonly>
            </div>
            <div class="buttons">
                <button value="AC">AC</button>
                <button value="CE">CE</button>
                <button value="%">%</button>
                <button value="/">/</button>
                <button value="7">7</button>
                <button value="8">8</button>
                <button value="9">9</button>
                <button value="*">*</button>
                <button value="4">4</button>
                <button value="5">5</button>
                <button value="6">6</button>
                <button value="-">-</button>
                <button value="1">1</button>
                <button value="2">2</button>
                <button value="3">3</button>
                <button value="+">+</button>
                <button value=".">.</button>
                <button value="0">0</button>
                <button class="sum" value="=">=</button>
            </div>
        </div>

        <script src='http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js'></script>
        <script>
          $(document).ready(function(){
              var sum = '';
              var clear = false;
              var calc = '';
              $('button').click(function(){
                  var text = $(this).attr('value');
                  if(parseInt(text, 10) == text || text === '.' || text === '/' || text === '*' || text ==='+' || text === '-' || text === '%'){
                      if(clear === false){
                          calc += text;
                          $('#textbox').val(calc);
                      }else{
                          calc = text;
                          $('#textbox').val(calc);
                          clear = false;
                      }
                  }else if(text === 'AC'){
                      calc = '';
                      $('#textbox').val('');
                  }else if(text == 'CE'){
                      calc = calc.slice(0, -1);
                      $('#textbox').val(calc);
                  }else if(text === '='){
                      sum = eval(calc);
                      $('#textbox').val(sum);
                      clear = true;
                  }
              })
          });
        </script>
    </body>
</html>

 

posted @ 2017-01-02 20:25  chigga  阅读(226)  评论(0编辑  收藏  举报