Blogger Information
Blog 36
fans 0
comment 0
visits 28470
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
用JS完成一个计算器——2018年9月12日
Jackson
Original
602 people have browsed it

这个计算器比较简单,通过获取表单上的操作数和操作符,然后再进行相应的运算,在把运算结果显示在页面上。

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算机</title>
    <style>
        .box{
            text-align:center;
            color: #636363;
            width:500px;
            height: 200px;
            background: #efefef;
            border: 1px solid lightgray;
            margin: 20px auto;
            box-shadow: 2px 2px 2px #636363;
            border-radius: 15px;
        }
        table{
            margin:  auto;
        }
        table tr td{
           width: 100px;
            height: 30px;
            padding: 5px 10px;
        }
        input, select{
            width:100%;
            height: 100%;
            border: none;
            padding-left: 15px;
        }
        button {
            width: 80%;
            height: 100%;
            border: none;
            background-color: skyblue;
            color: white;
        }

        button:hover {
            cursor: pointer;
            background-color: coral;
            width: 100%;
            height: 110%;
        }
    </style>
</head>
<body>
    <div class="box">
        <h2>计算器</h2>
        <form action="" method="get">
            <table>
                <tr>
                    <td><input type="text" name="op1" placeholder="操作数1"></td>
                    <td>
                        <select name="option" id="">
                            <option value="null">请选择操作</option>
                            <option value="add">+</option>
                            <option value="sub">-</option>
                            <option value="mul">*</option>
                            <option value="div">/</option>
                        </select>
                    </td>
                    <td ><input type="text" name="op2" placeholder="操作数2"></td>
                    <td><button type="button">计算</button></td>
                </tr>
                <tr>
                    <td colspan="2" align="right"><h3>结果:</h3></td>
                    <td colspan="2" align="left"><H3 id="result"></H3></td>
                </tr>
            </table>
        </form>
    </div>
</body>
</html>
<script>
    //获取操作数 按钮 结果占位符 这些都是对象 并不是数值
    let op1 = document.getElementsByName('op1')[0];
    let op2 = document.getElementsByName('op2')[0];
    let opt = document.getElementsByName('option')[0];
    let result = document.getElementById('result');
    let btn = document.getElementsByTagName('button')[0];
    
    //给按钮添加事件 进行计算
   btn.onclick = function () {
       let data1 = 0;
       let data2 = 0;
       //判断操作数1是否为空
       if(op1.value.length === 0){
           alert('操作数1不能为空');
           return false;
       }else if(isNaN(op1.value)){
            alert('操作数1必须为数字');
            op1.focus();//获取焦点
           return false;
       }else if (op2.value.length === 0){
           alert('操作数2不能为空');
           return false ;
       }else if (isNaN(op2.value)) {
           alert('操作数2必须为数字');
           return false ;
       }else{
           data1 = parseFloat(op1.value);//parsefloat() 解析为浮点数
           data2 = parseFloat(op2.value);
       }

       //对操作符进行处理
       let option = opt.value;
       let temp = 0; //临时存放结果
       let flag = ''; //存放符号
       switch (option){
           case 'null':
               alert('请选择操作类型');
               opt.focus();
               return false;
           case 'add':
               flag = '+';
               temp = data1 + data2;
               break;
           case 'sub':
               flag = '-';
               temp = data1 - data2;
               break;
           case 'mul':
               flag = '*';
               temp = data1 * data2;
               break;
           case 'div':
               flag = '/';
               if (data2 === 0){
                   alert('除数不能为零');
                   op2.focus();
                   op2.value = '';
                   return false;
               }else{
                   temp = data1 /data2;
                   //四舍五入 保留两位小数
                   temp = Math.round(temp*100) / 100;
               }
               break;
       }

       let str = '';
       str = data1 + flag + data2 + '='+ temp;
       result.innerHTML = str;
   }
</script>

运行实例 »

点击 "运行实例" 按钮查看在线实例

总结:

这个案例虽然简单,但运用到的知识点还是蛮多的。用js获取页面元素的函数有挺多的,同时还了解到事件就是页面的交互,另外获取到的只是元素这个对象,并不是值,数值要用.value  来访问。

接收完数据还要进行相应的判断,操作符的处理用switch比较方便。

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments