计算器可键盘操作

<!DOCTYPE html>

<html>

    <head>

            <title>计算器</title>

            <style type="text/css">

                    *{

                            margin:0;

                            padding:0;

                            box-sizing: border-box;

                            font: 18px Arial,sans-serif;

                    }

                body{

                      height:100%;

                      background-color:lightslategrey;

                }

                #calculator{

                      margin: 15px auto;

                      width:330px;

                      height:400px;

                      border: 1px solid lightgray;

                      border-radius:32px;

                      background-color:pink;

                      padding:15px;

                }

            .LOGO{

                      height:8px;

                }

            .circle{

                    height:16px;

                    width:16px;

                    border-radius:12px;

                    margin-right: 16px;

                    background-color:lightgrey;

                    border:1px solid black;

                    display:inline-block;

            }

        input{

                  background-color: lightskyblue;

        }

        .screen{

                  margin-top:15px;

                  width:300px;

                  height:40px;

                  text-align: right;

                  padding-right:10px;

                  font-size:20px;

            }

        #keys{

                  border:1px solid lightgray;

                  height:223px;

                  margin-top:25px;

                  padding:8px;

        }

        #keys .buttons{

                  float:left;

                  width: 42px;

                  height: 36px;

                  border-radius:12px;

                  text-align:center;

                  background-color:lightgray;

                  margin: 0 17px 20px 0;

        }

        </style>

    </head>


<body onload="onLoad()">

    <div id="calculator">

              <div class="LOGO">

                    <div class="circle"></div>

                    <div class="circle"></div>

                    <div class="circle"></div>

                    ❤  ❤  ❤  ❤  ❤  ❤  ❤  ✌

              </div>

            <!--输入栏-->

            <div class="screen">

               <input type="text"id="screenName" name="screenName" class="screen" value=""                                         onkeyup="deleteEq()" onkeydown="onInput()"/>

            </div>

          <div id="keys" >

                  <!--第一排-->

                <input type="button" id="7" onclick="jsq(this.id)" value="7" class="buttons">

                <input type="button" id="8" onclick="jsq(this.id)" value="8" class="buttons">

                <input type="button" id="9" onclick="jsq(this.id)" value="9" class="buttons">

                <input type="button" id="Back" onclick="clearOne()" value="⇦" class="buttons">

                <input type="button" id="C" onclick="clearAll()" value="AC" class="buttons" style="margin-right:0px">

            <!--第二排-->

            <input type="button" id="4" onclick="jsq(this.id)" value="4" class="buttons">

            <input type="button" id="5" onclick="jsq(this.id)" value="5" class="buttons">

            <input type="button" id="6" onclick="jsq(this.id)" value="6" class="buttons">

            <input type="button" id="*" onclick="jsq(this.id)" value="*" class="buttons">

            <input type="button" id="/" onclick="jsq(this.id)" value="/" class="buttons" style="margin-right:0px">

            <!--第三排-->

            <input type="button" id="1" onclick="jsq(this.id)" value="1" class="buttons">

            <input type="button" id="2" onclick="jsq(this.id)" value="2" class="buttons">

            <input type="button" id="3" onclick="jsq(this.id)" value="3" class="buttons">

            <input type="button" id="+" onclick="jsq(this.id)" value="+" class="buttons">

            <input type="button" id="-" onclick="jsq(this.id)" value="-" class="buttons" style="margin-right:0px">

            <!--第四排-->

            <input type="button" id="0" onck="jsq(this.id)" value="0" class="buttons">

            <input type="button" id="00" onclick="jsq(this.id)" value="00" class="buttons">

            <input type="button" id="." onclick="jsq(this.id)" value="." class="buttons">

            <input type="button" id="%" onclick="jsq(this.id)" value="%" class="buttons">

            <input type="button" id="eva" onclick="eq()" value="=" class="buttons" style="margin-right:0px">

        </div>

     </div>

</body>

<script>

  var num = 0; // 定义第一个输入的数据

  function onInput(){

        if(event.keyCode==187){

        eq();

        }

  }

 function deleteEq(){

        if(event.keyCode==187){

        var temp=document.getElementById('screenName').value;

        document.getElementById('screenName').value=temp.substr(0,temp.length-1);

        }

  }

 function jsq(num) {

    //获取当前输入

    document.getElementById('screenName').value += document.getElementById(num).value;

  }

  function eq() {

    //计算输入结果

    document.getElementById("screenName").value = eval(document.getElementById("screenName").value);

  }

  function clearAll() {

    //清0

    document.getElementById("screenName").value = null;

    document.getElementById("screenName").focus();

  }

  function clearOne() {

    //退格

    var arr = document.getElementById("screenName");

    arr.value = arr.value.substring(0, arr.value.length - 1);

  }

  function onLoad(){

    //加载完毕后光标自动对应到输入框

    document.getElementById("screenName").focus();

  }

</script>

</html>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容