JavaScript实现简易计算器

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>IFE ECMAScript</title>
</head>

<body>
    <input id="first-number" type="number" value="0" placeholder="第一个数字">
    <input id="second-number" type="number" value="0" placeholder="第二个数字">
    <button id="add-btn" οnclick="add()">加</button>
    <button id="minus-btn" οnclick="minus()">减</button>
    <button id="times-btn" οnclick="multiply()">乘</button>
    <button id="divide-btn" οnclick="divide()">除</button>
    <p id="result">运算结果</p>
    <script>
        function add() {
            document.getElementById("result").innerHTML = (parseInt(document.getElementById("first-number").value) + parseInt(document.getElementById("second-number").value)); 
        }
        function minus() {
            document.getElementById("result").innerHTML = (parseInt(document.getElementById("first-number").value) - parseInt(document.getElementById("second-number").value)); 
        }
        function multiply() {
            document.getElementById("result").innerHTML = (parseInt(document.getElementById("first-number").value) * parseInt(document.getElementById("second-number").value)); 
        }
        function divide() {
            document.getElementById("result").innerHTML = (parseInt(document.getElementById("first-number").value) / parseInt(document.getElementById("second-number").value)); 
        }
    </script>
</body>

</html>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>IFE ECMAScript</title>
</head>

<body>
    <input id="first-number" type="number" value="0" placeholder="第一个数字">
    <input id="second-number" type="number" value="0" placeholder="第二个数字">
    <button id="add-btn">加</button>
    <button id="minus-btn">减</button>
    <button id="times-btn">乘</button>
    <button id="divide-btn">除</button>
    <p id="result">运算结果</p>
    <script>
        var btn1 = document.querySelector("#add-btn");
        var btn2 = document.querySelector("#minus-btn");
        var btn3 = document.querySelector("#times-btn");
        var btn4 = document.querySelector("#divide-btn");
        function add() {
            document.getElementById("result").innerHTML = (parseInt(document.getElementById("first-number").value) + parseInt(document.getElementById("second-number").value)); 
        }
        function minus() {
            document.getElementById("result").innerHTML = (parseInt(document.getElementById("first-number").value) - parseInt(document.getElementById("second-number").value)); 
        }
        function times() {
            document.getElementById("result").innerHTML = (parseInt(document.getElementById("first-number").value) * parseInt(document.getElementById("second-number").value)); 
        }
        function divide() {
            document.getElementById("result").innerHTML = (parseInt(document.getElementById("first-number").value) / parseInt(document.getElementById("second-number").value)); 
        }
        btn1.addEventListener("click", add);
        btn2.addEventListener("click", minus);
        btn3.addEventListener("click", times);
        btn4.addEventListener("click", divide);
    </script>
</body>

</html>

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>IFE ECMAScript</title>
</head>

<body>
    <input id="first-number" type="number" value="0" placeholder="第一个数字">
    <input id="second-number" type="number" value="0" placeholder="第二个数字">
    <button id="add-btn">加</button>
    <button id="minus-btn">减</button>
    <button id="times-btn">乘</button>
    <button id="divide-btn">除</button>
    <p id="result">运算结果</p>
    <script>
        var btn1 = document.querySelector("#add-btn");
        var btn2 = document.querySelector("#minus-btn");
        var btn3 = document.querySelector("#times-btn");
        var btn4 = document.querySelector("#divide-btn");
        function add() {
            document.getElementById("result").innerHTML = (parseInt(document.getElementById("first-number").value) + parseInt(document.getElementById("second-number").value)); 
        }
        function minus() {
            document.getElementById("result").innerHTML = (parseInt(document.getElementById("first-number").value) - parseInt(document.getElementById("second-number").value)); 
        }
        function times() {
            document.getElementById("result").innerHTML = (parseInt(document.getElementById("first-number").value) * parseInt(document.getElementById("second-number").value)); 
        }
        function divide() {
            var divisor = parseInt(document.getElementById("second-number").value);
            try {
                if (divisor == 0) {
                    throw "除数不能为零!";
                }
            } catch (error) {
                console.log(error);
            }
            document.getElementById("result").innerHTML = (parseInt(document.getElementById("first-number").value) / divisor); 
        }
        btn1.addEventListener("click", add);
        btn2.addEventListener("click", minus);
        btn3.addEventListener("click", times);
        btn4.addEventListener("click", divide);
    </script>
</body>

</html>

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>IFE ECMAScript</title>
</head>

<body>
    <input id="first-number" type="number" value="0" placeholder="第一个数字">
    <input id="second-number" type="number" value="0" placeholder="第二个数字">
    <button id="add-btn">加</button>
    <button id="minus-btn">减</button>
    <button id="times-btn">乘</button>
    <button id="divide-btn">除</button>
    <p id="result">运算结果</p>
    <script>
        var btn1 = document.querySelector("#add-btn");
        var btn2 = document.querySelector("#minus-btn");
        var btn3 = document.querySelector("#times-btn");
        var btn4 = document.querySelector("#divide-btn");
        function add() {
            return (parseInt(document.getElementById("first-number").value) + parseInt(document.getElementById("second-number").value)); 
        }
        function minus() {
            return (parseInt(document.getElementById("first-number").value) - parseInt(document.getElementById("second-number").value)); 
        }
        function times() {
            return (parseInt(document.getElementById("first-number").value) * parseInt(document.getElementById("second-number").value)); 
        }
        function divide() {
            var divisor = parseInt(document.getElementById("second-number").value);
            try {
                if (divisor == 0) {
                    throw "除数不能为零!";
                }
            } catch (error) {
                console.log(error);
            }
            return (parseInt(document.getElementById("first-number").value) / divisor); 
        }
        btn1.addEventListener("click", add);
        btn2.addEventListener("click", minus);
        btn3.addEventListener("click", times);
        btn4.addEventListener("click", divide);
        btn1.addEventListener("click", function() {
            document.getElementById("result").innerHTML = add();
        });
        btn2.addEventListener("click", function() {
            document.getElementById("result").innerHTML = minus();
        });
        btn3.addEventListener("click", function() {
            document.getElementById("result").innerHTML = times();
        });
        btn4.addEventListener("click", function() {
            document.getElementById("result").innerHTML = divide();
        });
    </script>
</body>

</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值