Js实现简单计算器

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>计算器的实现</title>
</head>
<style type="text/css">
   
    #sDiv {
        text-align: center;
        border: solid 1px;
        width: 300px;
        border-radius: 10px;
        background-color: aqua;
        margin: 20px auto;
        background-image: url("http://b-ssl.duitang.com/uploads/item/201506/09/20150609224048_RCQPf.jpeg") ;
    }

    #t {
        border: solid 1px;
        width: 260px;
        border-radius: 10px;
        margin-top: 10px;
        margin-bottom: 10px;
        font-size: 20px;
    }

    input[type=button] {
        border-radius: 15px;
        width: 55px;
        height: 30px;
        margin: 2px;
        font-size: 20px;
    }

    #equ {
        width: 260px;
        font-size: 30px;
        margin-bottom: 10px;
    }
</style>

<body>
<div id="sDiv">

    <input type="text" name="t" id="t" value=""/><br/>
    <input type="button" name="" value="1" onclick="calCulate(this.value)"/>
    <input type="button" name="" value="2" onclick="calCulate(this.value)"/>
    <input type="button" name="" value="3" onclick="calCulate(this.value)"/>
    <input type="button" name="" value="4" onclick="calCulate(this.value)"/><br/>
    <input type="button" name="" value="5" onclick="calCulate(this.value)"/>
    <input type="button" name="" value="6" onclick="calCulate(this.value)"/>
    <input type="button" name="" value="7" onclick="calCulate(this.value)"/>
    <input type="button" name="" value="8" onclick="calCulate(this.value)"/><br/>
    <input type="button" name="" value="9" onclick="calCulate(this.value)"/>
    <input type="button" name="" value="0" onclick="calCulate(this.value)"/>
    <input type="button" name="" value="." onclick="calCulate(this.value)"/>
    <input type="button" name="" value="C" onclick="calCulate(this.value)"/><br/>
    <input type="button" name="" value="+" onclick="calCulate(this.value)"/>
    <input type="button" name="" value="-" onclick="calCulate(this.value)"/>
    <input type="button" name="" value="*" onclick="calCulate(this.value)"/>
    <input type="button" name="" value="/" onclick="calCulate(this.value)"/><br/>
    <input type="button" name="" id="equ" value="=" onclick="calCulate(this.value)"/>
</div>
<script type="text/javascript">

    function calCulate(val) {

        console.log("===>",val);

        var num = document.getElementById("t");
        switch (val) {
            case "=":
                num.value = eval(num.value);

                break;

            case "C":
                num.value = "";
                break;

            default:
                num.value = num.value + val;
                break;
        }


    }
</script>
</body>
</html>

需要讨论 解决问题和获取学习资料可以加群 1022848328:一起学习

  • 18
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值