HTML+CSS+Javascript 实现简单计算器

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>简单计算器</title>
    <style>
        *{
            box-sizing: border-box;
        }
        .mycalculator{
            border: 2px solid black;
            width: 300px;
            border-radius: 30px;
            background-color:burlywood;
            padding-left: 60px;
            margin:auto;
            margin-top: 150px;
            box-shadow:5px 5px gray;
        }
        .wenben{
            size:15px;
        }
        .buto{
            width:40px;
            height:40px;
            margin: inherit;
        }
        .operator{
            width:40px;
            height:40px;
            margin: inherit;
        }
        .buto,.wenben,.operator{
            border-radius:10px;
        }
        #display{
            height:40px;
            background-color: #98d1dc;
            box-shadow:inset 5px 5px darkolivegreen;
        }
    </style>
</head>

<body>
    <div class="mycalculator">
        <h3>by 一笑而过</h3>

        <form class="calculator">
            <input type="button"  class="operator" id="clear" value="清空">   <!--清空display中的内容-->
            <input type="text"  id="display">               <!--文本框-->
            <br>

            <input type="button" class="buto" value="7" onclick="getvalue(this.value)">
            <input type="button" class="buto" value="8" onclick="getvalue(this.value)">
            <input type="button" class="buto" value="9" onclick="getvalue(this.value)">
            <input type="button" class="operator" value="+" onclick="getvalue(this.value)">
            <br>

            <input type="button" class="buto" value="4" onclick="getvalue(this.value)">
            <input type="button" class="buto" value="5" onclick="getvalue(this.value)">
            <input type="button" class="buto" value="6" onclick="getvalue(this.value)">
            <input type="button" class="operator" value="-" onclick="getvalue(this.value)">
            <br>

            <input type="button" class="buto" value="1" onclick="getvalue(this.value)">
            <input type="button" class="buto" value="2" onclick="getvalue(this.value)">
            <input type="button" class="buto" value="3" onclick="getvalue(this.value)">
            <input type="button" class="operator" value="*" onclick="getvalue(this.value)">
            <br>

            <input type="button" class="buto" value="0" onclick="getvalue(this.value)">
            <input type="button" class="operator" value="." onclick="getvalue(this.value)">
            <input type="button" class="operator" value="/" onclick="getvalue(this.value)">
            <input type="button" class="operator" id="result" value="=">
            <br>
        </form>

    </div>

    <script>
        var body_Element = document.getElementsByTagName("body")[0];
        document.getElementById("clear").onclick = function() {
            document.getElementById("display").value="";
        };

        function getvalue(value){       
            document.getElementById("display").value += value;
        }

        document.getElementById("result").onclick = function() {
            var answer = 0;           
            answer = document.getElementById("display").value;
            document.getElementById("display").value = "";
            document.getElementById("display").value = eval(answer);      //计算表达式或者语句
        };

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值