zoukankan      html  css  js  c++  java
  • 网页计算器简单实现加减乘除功能

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>网页计算器</title>
    </head>
    <body>
        <p>整数1:<input type="text" id="num1"></p>
        <p>整数2:<input type="text" id="num2"></p>
        <p>
            <input type="button" value="相加" onclick="calc(add)">
            <input type="button" value="相减" onclick="calc(sub)">
            <input type="button" value="相乘" onclick="calc(mul)">
            <input type="button" value="相除" onclick="calc(div)">
        </p>
        <p>结果:<input type="text" id="result" readonly></p>
        
    </body>
    </html>
    <script>
            function calc(func){
                var result = document.getElementById('result');
                var num1 = parseInt(document.getElementById('num1').value);
                var num2 = parseInt(document.getElementById('num2').value);
                if(isNaN(num1) || isNaN(num2)){
                    alert('请输入数字');
                    return false;
                }
                result.value = func(num1,num2);
            }
            function add(num1,num2){
                return num1+num2;
            }
            function sub(num1,num2){
                return num1-num2;
            }
            function mul(num1,num2){
                return num1*num2;
            }
            function div(num1,num2){
                if(num2===0){
                    alert('除数不能为零');
                    return '';
                }
                return num1/num2;
            }
        </script>
    

      

  • 相关阅读:
    mysql安装与基本管理,mysql密码破解
    非阻塞IO模板
    多路复用IO模板
    第四模块:网络编程进阶&数据库开发 练习
    理解Queue队列中join()与task_done()的关系
    第四模块:网络编程进阶&数据库开发 口述
    SQLite
    rest_cherrypy
    SaltStack Returners
    kafka集群安装,配置
  • 原文地址:https://www.cnblogs.com/xianggua/p/11121840.html
Copyright © 2011-2022 走看看