Blogger Information
Blog 38
fans 1
comment 0
visits 26001
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
javascript中的DOM事件--2018年09月13日13时38分
一根火柴棒的博客
Original
566 people have browsed it

使用js完成一个功能相对完整的计算器案例:

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算器</title>
</head>
<body>

<style>

    .box {
        width: 500px;
        height: 200px;
        background-color: lightblue;
        border: 1px solid #efefef;
        border-radius: 15px;
        box-shadow: 2px 2px 2px #999;
    }

</style>


<div class="box">

    <table>
        <caption>计算器</caption>
        <tr>
            <td><input type="text" name="opt1" placeholder="第一个操作数"></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="opt2" placeholder="第二个操作数"></td>
            <td><button type="button">计算</button></td>
        </tr>

        <tr>
            <td align="right" colspan="2"><h3>计算结果:</h3></td>
            <td align="left" colspan="2"><h3 id="result">15</h3></td>
        </tr>

    </table>

</div>



<script>
    //获取变量
    let opt1 = document.getElementsByName('opt1')[0];
    let opt2 = document.getElementsByName('opt2')[0];
    let opt = document.getElementsByName('option')[0];
    let btn = document.getElementsByTagName('button')[0];
    let result = document.getElementById('result');

    btn.onclick = function (){

        let data1 = 0;
        let data2 = 0;

        if (opt1.value.length === 0) //判断文本框内容长度
        {
            alert('第一个操作数不能为空');
            opt1.focus();
            return false;
        } else if (isNaN(opt1.value)) //判断文本框内容是否为数字
        {
            alert('第一个操作数必须为数字');
            opt1.focus();
            return false;
        } else if (opt2.value.length === 0)
        {
            alert('第二个操作数不能为空');
            opt2.focus();
            return false;
        } else if (isNaN(opt2.value))
        {
            alert('第二个操作数必须为数字');
            opt2.focus();
            return false;
        } else {
            data1 = parseFloat(opt1.value);
            data2 = parseFloat(opt2.value);
        }

    //对操作符进行处理
        let option = opt.value;
        let tmp = 0;
        let flag = '';

        switch (option) {
            case 'null':
                alert('请选择操作符');
                opt.focus();
                return false;
            case 'add' :
                flag = '+';
                tmp = data1 + data2;
                break;
            case 'sub':
                flag = '-';
                tmp = data1 - data2;
                break;
            case 'mul':
                flag = '*';
                tmp = data1 * data2;
                break;
            case 'div':
                flag = '/';
                if (data2 === 0){
                    alert('除数不能为0');
                    opt2.focus();
                    return false
                }

                tmp = data1 / data2;
                tmp = Math.round(tmp *100) / 100;
                break;
        }

        result.innerHTML = tmp;
    }



</script>


</body>
</html>

运行实例 »

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


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