• JS简易计算器的实现,以及代码的优化


    用JS实现简易计算器

    首先创建结构和样式

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            body{
                margin:0;
                padding:0;
                background-color: #abcdef;
            }
            .cal{
                width:560px;
                height:100px;
                margin:0 auto;
                padding-top:300px;
            }
        </style>
    </head>
    <body>
    
        <div class="cal">
            <p>
                <input type="text" class="num1" value="1">
                <span class="sign">+</span>
                <input type="text" class="num2" value="1">
                <span>=</span>
                <span class="res">2</span>
            </p>
            <p>
                <input type="button" value="+">
                <input type="button" value="-">
                <input type="button" value="*">
                <input type="button" value="/">
            </p>            
        </div>
    <script>
    
    
    </script>
    
    </body>
    </html>

     添加javascript功能

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            body{
                margin:0;
                padding:0;
                background-color: #abcdef;
            }
            .cal{
                width:560px;
                height:100px;
                margin:0 auto;
                padding-top:300px;
            }
        </style>
    </head>
    <body>
    
        <div class="cal">
            <p>
                <input type="text" class="num1" value="1">
                <span class="sign">+</span>
                <input type="text" class="num2" value="1">
                <span>=</span>
                <span class="res">2</span>
            </p>
            <p>
                <input type="button" value="+" class="btn">
                <input type="button" value="-" class="btn">
                <input type="button" value="*" class="btn">
                <input type="button" value="/" class="btn">
            </p>            
        </div>
    
    <script>
        var cal=document.querySelector(".cal");
        var num1=cal.querySelector(".num1");
        var num2=cal.querySelector(".num2");
        var sign=cal.querySelector(".sign");
        var res=cal.querySelector(".res");
    
        var btns=cal.querySelectorAll(".btn");
    
        btns[0].onclick=add;
        btns[1].onclick=subtract;
        btns[2].onclick=multiply;
        btns[3].onclick=divide;
    
        function add(){
            sign.innerHTML="+";
            //由于DOM获取到的值都是字符串形式,因此进行操作时需要先转为number类型
            res.innerHTML=Number(num1.value)+Number(num2.value);
        }
        function subtract(){
            sign.innerHTML="-";
            res.innerHTML=Number(num1.value)-Number(num2.value);
        }
        function multiply(){
            sign.innerHTML="*";
            res.innerHTML=Number(num1.value)*Number(num2.value);    
        }
        function divide(){
            sign.innerHTML="/";
            res.innerHTML=Number(num1.value)/Number(num2.value);    
        }
    </script>
    
    </body>
    </html>

    代码的优化:

    循环实现绑定

    给一个外部接口,用于新增运算

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            body{
                margin:0;
                padding:0;
                background-color: #abcdef;
            }
            .cal{
                width:560px;
                height:100px;
                margin:0 auto;
                padding-top:300px;
            }
        </style>
    </head>
    <body>
    
        <div class="cal">
            <p>
                <input type="text" class="num1" value="1">
                <span class="sign">+</span>
                <input type="text" class="num2" value="1">
                <span>=</span>
                <span class="res">2</span>
            </p>
            <p>
                <input type="button" value="+" class="btn" title="add">
                <input type="button" value="-" class="btn" title="subtract">
                <input type="button" value="*" class="btn" title="multiply">
                <input type="button" value="/" class="btn" title="divide">
                <input type="button" value="%" class="btn" title="mod">
            </p>            
        </div>
    
    <script>
        var cal=document.querySelector(".cal");
        var num1=cal.querySelector(".num1");
        var num2=cal.querySelector(".num2");
        var sign=cal.querySelector(".sign");
        var res=cal.querySelector(".res");
    
        var btns=cal.querySelectorAll(".btn");
    
        //给每个按钮绑定事件
        for(var i=0;i<btns.length;i++){
            operate(i);
        }
    
        //运算函数
        function operate(i){
            var op=btns[i].value;//获取运算
            var opName=btns[i].title;//获取运算名
            //绑定事件
            btns[i].onclick=function(){
                sign.innerHTML=op;
                res.innerHTML=operation[opName](Number(num1.value),Number(num2.value));
            }
        }
    
        var operation={
            add:function(n1,n2){
                return n1+n2;
            },
            subtract:function(n1,n2){
                return n1-n2;
            },
            multiply:function(n1,n2){
                return n1*n2;
            },
            divide:function(n1,n2){
                return n1/n2;
            },
            //给一个新增运算的接口
            addOperation:function(name,fn){
                //如果该运算不存在
                if(!this.name){
                    this[name]=fn;
                }
            }
        }
    
        //新增取余运算
        operation.addOperation("mod",function(n1,n2){
            return n1%n2;
        });
    </script>
    
    </body>
    </html>

  • 相关阅读:
    ROS Learning-013 beginner_Tutorials (编程) 编写ROS服务版的Hello World程序(Python版)
    ROS Learning-012 beginner_Tutorials (编程) 创建自定义的ROS消息和ROS服务
    电子模块 001 --- 遥杆 JoyStick
    ROS Learning-011 beginner_Tutorials (编程) 编写 ROS 话题版的 Hello World 程序(Python版)
    STM32 C++编程 002 GPIO类
    Python 网络爬虫 002 (入门) 爬取一个网站之前,要了解的知识
    STM32 C++编程 001 工程模板
    ROS Learning-010 beginner_Tutorials 编写简单的启动脚本文件(.launch 文件)
    Python 网络爬虫 001 (科普) 网络爬虫简介
    Python 黑客 --- 002 入门级 ZIP压缩文件口令暴力破解机
  • 原文地址:https://www.cnblogs.com/chenyingying0/p/12300882.html
Copyright © 2020-2023  润新知