JS写计算器

<style>
        .con{
            border: 1px solid #000000;
            width: 500px;
            height: 600px;
            text-align: center;
            position: relative;
            left:0;
            right: 0;
            top: 0;
            bottom: 0;
            margin: auto;
        }
        .con>div{
            width: 100px;
            height: 100px;
            border: 1px solid #000000;
            box-shadow: 2px 2px 2px #CCCCCC;
            float: left;
            margin: 11px;
            text-align: center;
            font-size: 40px;
            line-height: 100px;
            user-select: none;
        }
        .con>input{
            width: 90%;
            height: 30px;
            line-height: 30px;
            font-size: 25px;
            text-align: right;
            margin-top: 20px;
            margin-bottom: 20px;
            box-shadow: 3px 3px 3px #CCCCCC;
            outline: none;
            padding: 5px 10px;
            background-color: white;
            border: 1px solid #000000;
        }
        .clear::after{
            content: "";
            clear: both;
            display: block;
            height: 0;
            visibility: hidden;
            overflow: hidden;
        }
        .clear{
            zoom: 1;
        }
</style>
<body>
    <div class="con clear">
        <input type="text" id="input" disabled>
        <div class="divs">7</div>
        <div class="divs">8</div>
        <div class="divs">9</div>
        <div class="divs">-</div>
        <div class="divs">4</div>
        <div class="divs">5</div>
        <div class="divs">6</div>
        <div class="divs">+</div>
        <div class="divs">1</div>
        <div class="divs">2</div>
        <div class="divs">3</div>
        <div class="divs">*</div>
        <div class="divs">C</div>
        <div class="divs">0</div>
        <div class="divs">=</div>
        <div class="divs">/</div>
    </div>


    <script>
           var input,divs,type;
           var value1="",value2="";
        
            init();
           function init(){
              input=document.getElementById("input");
              divs=document.getElementsByClassName("divs");
               for(var i=0;i<divs.length;i++){
                   divs[i].onclick=clickHandler
               }

           }

           function clickHandler(){
                // console.log(this.innerHTML);//暂时认知在点击的函数中this是被点击的元素
                if(isNaN(this.innerHTML)){
                    calculateValue(this.innerHTML);
                }else{
                    setInputValue(Number(this.innerHTML))
                }
           }

           function setInputValue(num){
                if(!type){
                    value1=Number(value1+num).toString();
                    input.value=value1;
                }else{
                    value2=Number(value2+num).toString();
                    input.value=value1+type+value2;
                }
           }

           function calculateValue(_type){
                if(_type==="C") return clearInput();//执行函数并且跳出当前函数,并且不继续向下执行
                if(_type==="=") return calculate();
                if(!value1)return;
                type=_type;
           }

           function clearInput(){
              value1="";
              value2="";
              type="";
              input.value="";
           }

           function calculate(){
               if(!type || !value2)return;
               var s;
                switch(type){
                    case "+":
                    s=Number(value1)+Number(value2);
                    break;
                    case "-":
                    s=value1-value2;
                    break;
                    case "*":
                    s=value1*value2;
                    break;
                    case "/":
                    s=value1/value2;
                    break;
                }
                input.value=value1+type+value2+"="+s;
                value1=s.toString();
                value2="";
                type="";
           }
    </script>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值