JS计算器

 <style type="text/css">
        .style1 {
            width: 23%;
            height: 296px;
            background-color:Black;
        }
        #txt_Result
        {
            height: 26px;
            width: 191px;
            text-align:right;
            font-size:large;
        }
        input
        {
            width:50px;
            height:25px;
            }
    </style>
    <script type="text/javascript">
        var n;   //保存点击运算符前输入框中数字
        var o;   //存储对应运算符

        function s(obj) { //数字键对应事件
            var txt = document.getElementById("txt_Result");
            
            if (txt.value.indexOf('.') > -1 && obj == '.') {
                return false;
            }
            txt.value += obj;
        }
        function c(op) {  //运算符(+,-,*,%)对应事件
            n = document.getElementById('txt_Result').value;
            document.getElementById('txt_Result').value = "";
            o = op.value;
        }
        function qc() {    //清除键对应事件
            document.getElementById('txt_Result').value = "";
            n = "";
            o = "";
        }
        function e() { //等号对应事件
            var s = document.getElementById('txt_Result');
            if (o == '+') {
                s.value =parseFloat(s.value) + parseFloat(n);
            }
            else if (o == '-') {
                s.value = parseFloat(n) - parseFloat(s.value);
            }
            else if (o == '*') {
                s.value = parseFloat(s.value) * parseFloat(n);
            }
            else {
                s.value = parseFloat(n) / parseFloat(s.value);
            }
            n = "";
            o = "";
        }
        function b() { //退格键对应事件
            var s = document.getElementById('txt_Result');
            s.value = s.value.substring(0, s.value.length - 1);
        }
        function ds() {    //求倒数键对应事件
            var s = document.getElementById('txt_Result');
            s.value = 1 / parseFloat(s.value);
        }
        function square() {   //平方键对应事件
            var s = document.getElementById('txt_Result');
            s.value = parseFloat(s.value) * parseFloat(s.value);
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    
        <table class="style1">
            <tr>
                <td colspan="4">
                    <input id="txt_Result" type="text" /></td>
            </tr>
            <tr>
                <td>
                    <input id="Button1" type="button" value="7" οnclick="s('7')"/></td>
                <td>
                    <input id="Button2" type="button" value="8" οnclick="s('8')"/></td>
                <td>
                    <input id="Button3" type="button" value="9" οnclick="s('9')"/></td>
                <td>
                    <input id="Button4" type="button" value="←" οnclick="b()"/></td>
            </tr>
            <tr>
                <td>
                    <input id="Button8" type="button" value="4" οnclick="s('4')"/></td>
                <td>
                    <input id="Button7" type="button" value="5" οnclick="s('5')"/></td>
                <td>
                    <input id="Button6" type="button" value="6" οnclick="s('6')"/></td>
                <td>
                    <input id="Button5" type="button" value="+" οnclick="c(this)"/></td>
            </tr>
            <tr>
                <td>
                    <input id="Button9" type="button" value="1" οnclick="s('1')"/></td>
                <td>
                    <input id="Button10" type="button" value="2" οnclick="s('2')"/></td>
                <td>
                    <input id="Button11" type="button" value="3" οnclick="s('3')"/></td>
                <td>
                    <input id="Button12" type="button" value="-" οnclick="c(this)"/></td>
            </tr>
            <tr>
                <td>
                    <input id="Button16" type="button" value="0" οnclick="s('0')"/></td>
                <td>
                    <input id="Button15" type="button" value="." οnclick="s('.')"/></td>
                <td>
                    <input id="Button14" type="button" value="=" οnclick="e()"/></td>
                <td>
                    <input id="Button13" type="button" value="*" οnclick="c(this)"/></td>
            </tr>
            <tr>
                <td>
                    <input id="Button17" type="button" value="1/x" οnclick="ds()"/></td>
                <td>
                    <input id="Button18" type="button" value="x2" οnclick="square()"/></td>
                <td>
                    <input id="Button19" type="button" value="Clear" οnclick="qc()"/></td>
                <td>
                    <input id="Button20" type="button" value="%" οnclick="c(this)"/></td>
            </tr>
        </table>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值