JavaScript编写简单的网页计算器

计算器app如下:

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

代码语言:javascript
复制
<!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);}
&lt;/style&gt;
&lt;script type=&#34;text/javascript&#34; src=&#34;https://code.jquery.com/jquery-3.1.1.min.js&#34; &gt;&lt;/script&gt;
&lt;script&gt;
    $(document).ready(function () {
            var clickBtn = &#34;0&#34;;//结果显示内容 x÷
            var calstr = &#34;&#34;;//实际计算的内容 */
            $(&#34;:button&#34;).click(function () {
                var n = $(this).val();
                if (n == &#39;C&#39;) {
                    clickBtn = &#34;0&#34;;
                } else if (n == &#39;=&#39;) {
                    try {
                        var inputText = $(&#34;#result&#34;).val();
                        calstr = inputText.replace(/x/g, &#34;*&#34;).replace(/÷/g, &#34;/&#34;);
                        clickBtn = window.eval(calstr);
                    } catch (err) {
                        clickBtn = &#34;输入错误!&#34;;
                    }
                } else if ($(&#34;#result&#34;).val() == 0 &amp;&amp; n != &#34;+&#34; &amp;&amp; n != &#34;-&#34; &amp;&amp; n != &#34;x&#34; &amp;&amp; n != &#34;÷&#34;) {
                    clickBtn = n;
                } else {
                    clickBtn += n;
                }
                $(&#34;#result&#34;).val(clickBtn);
            });
        });
&lt;/script&gt;

</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>