JavaScript编写简单的网页计算器

8/14/2022 html编程
(adsbygoogle = window.adsbygoogle || []).push({});

计算器app如下:

学习了js和jquery,编写了一个计算器app。主要思路是将按键事件对应字符串存储,利用eval()方法进行执行,需要注意的时执行前要将x替换为*,÷替换为/。另外调整了一些CSS样式,模仿iPhone计算器的界面,源代码看嵌入的网页。


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计算器</title>
    <style>
        *{margin: 0px;padding: 0px;}
        #main{background: #000;border-radius: 30px; height: 500px;width: 220px;margin: auto;}
        #result{width: 165px;height: 40px; margin: 60px 0px 5px 25px; border: 0px;
            background: #000;color: white;text-align: right;line-height: 40px;font-size: 40px;}

        table{background: #000;border-spacing:6px ;margin: auto; }
        table input{width:40px ;height: 40px; text-align: center;background: rgb(49, 49, 49);border:solid 0px blac;border-radius: 20px;
            color: white;font-size: 20px; line-height: 40px;}
        table input:active{background: #fff;color: #000;}    
        
        .btnf{background: rgb(246, 153, 6)}
        .btnf:active{background: rgb(246, 246, 246);color: rgb(246, 153, 6);}
        #cal{background: rgb(183, 45, 26);width: 80px;}
        #cal:active{background:rgb(246, 246, 246);color: rgb(183, 45, 26);}
    </style>
    <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js" ></script>
    <script>
        $(document).ready(function () {
                var clickBtn = "0";//结果显示内容 x÷
                var calstr = "";//实际计算的内容 */
                $(":button").click(function () {
                    var n = $(this).val();
                    if (n == 'C') {
                        clickBtn = "0";
                    } else if (n == '=') {
                        try {
                            var inputText = $("#result").val();
                            calstr = inputText.replace(/x/g, "*").replace(/÷/g, "/");
                            clickBtn = window.eval(calstr);
                        } catch (err) {
                            clickBtn = "输入错误!";
                        }
                    } else if ($("#result").val() == 0 && n != "+" && n != "-" && n != "x" && n != "÷") {
                        clickBtn = n;
                    } else {
                        clickBtn += n;
                    }
                    $("#result").val(clickBtn);
                });
            });
    </script>
</head>
<body>
    <div id="main">
    <input type="text" id="result" value="0" size="17" maxlength="17" >
    <table border="0px" >
        <tr>
            <td><input type="button" class="btn" value="7"></td>
            <td><input type="button" class="btn" value="8"></td>
            <td><input type="button" class="btn" value="9"></td>
            <td><input type="button"  id="add" class="btnf" value="+"></td>
        </tr>
        <tr>
            <td><input type="button" class="btn" value="4"></td>
            <td><input type="button" class="btn" value="5"></td>
            <td><input type="button" class="btn" value="6"></td>
            <td><input type="button"  id="sub" class="btnf" value="-"></td>
        </tr>
        <tr>
            <td><input type="button" class="btn" value="1"></td>
            <td><input type="button" class="btn" value="2"></td>
            <td><input type="button" class="btn" value="3"></td>
            <td><input type="button"  id="times" class="btnf" value="x"></td>
        </tr>
        <tr>
            <td><input type="button" class="btn" value="0"></td>
            <td><input type="button" class="btn" id="dot" value="."></td>
            <td><input type="button" class="btnf" value="C"></td>
            <td><input type="button"  class="btnf" id="chu" value="÷"></td>
        </tr>
        <tr>
            <td><input type="button" class="btn" value="("></td>
            <td><input type="button" class="btn"  value=")"></td>
            <td colspan="2"><input type="button" class="btnf" id="cal" value="=" ></td>
        </tr>
    </table>
</div>
    
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89