Blogger Information
Blog 39
fans 2
comment 2
visits 50607
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript中的DOM与事件的基础操作-2018年9月12号
fighting的博客
Original
725 people have browsed it

                                                     JavaScript中的DOM与事件的基础操作

                                                    时间:2018年9月12号              天气:阴

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

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计算器</title>
    <style>
        .box{
            width: 500px;
            height: 200px;
            background-color: #efefef;
            text-align: center;
            border: 1px solid lightgray;
            margin: 20px auto;
            color: #636363;
            box-shadow: 2px 2px 2px #000;
            border-radius: 25px;
        }
        table {
            margin: auto;

        }
        td{
            width: 100px;
            height: 30px;
            padding: 5px 10px;
        }
       input,select{
           width: 100%;
           height: 100%;
           border: none;
           text-align: left;
           padding-left: 15px;
       }
        button{
            width: 100%;
            height: 100%;
            background-color: skyblue;
            color: white;
        }
        button:hover{
            width: 110%;
            height: 110%;
            background-color: coral;
            color: white;
        }
    </style>
</head>
<body>
<div class="box">
    <h2>计算器</h2>
    <form action="">
        <table>
        <tr>
            <td><input type="text" name="opt1" 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="opt2" placeholder="操作数2"></td>
            <td><button type="button">计算</button></td>
        </tr>
            <tr>
                <td align="right"><h3>结果:</h3></td>
                <td align="left" colspan="3"><h3 id="result"></h3></td>
            </tr>
        </table>
    </form>
</div>
<script>
    //1,获取操作数,按钮,结果,占位符
    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');
    //2,给按钮添加事件,进行计算
    btn.onclick = function () {
        //数据初始化
        let date1 =0;
        let date2 =0;
        //判断数据框,前端强烈推荐三个等号防止错误赋值.
        if(opt1.value.length === 0){
            alert('第一个操作数不能为空');
            opt1.focus();
            return false;
        }
        else if(opt2.value.length === 0){
            alert('第二个操作数不能为空');
            opt2.focus();
            return false;
        }
        else if(isNaN(opt1.value)){
            alert('第一个操作数不能为非数字');
            opt1.focus();
            return false;
        }
        else if(isNaN(opt2.value)){
            alert('第二个操作数不能为非数字');
            opt2.focus();
            return false;
        }else {
            //强制把opt1,2的值转换为数字
            date1 =parseFloat(opt1.value) ;
            date2 =parseFloat(opt2.value);
        }
        //拿到用户选择的操作
        let option =opt.value;
        let temp =0;
        let  flag = '';

        switch (option){
            case 'null':
                alert('请选择操作类型');
                opt.focus();
                return false;
            case 'add':
                flag = '+';
               temp = date1 + date2;
               break;
            case 'sub':
                flag = '-';
               temp = date1 - date2;
               break;
            case 'mul':
                flag = '*';
               temp = date2 * date1;
               break;
            case 'div':
                flag = '/';
                if (date2 === 0) {
                    alert('除数不能为零 ');
                    opt2.focus();
                    opt2.value = '';
                    return false;
                   }else{
                    temp = date1 / date2;
                    temp = Math.round(temp*100)/100;
                }
               break;
           }
        let  str = '<span style="color: green">';
        str += date1 +' '+flag +' '+ date2 + '=' +temp;
        str += '</span>';
        result.innerHTML = str;
    }
</script>
</body>
</html>

运行实例 »

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

1.png

总结:说来惭愧,看的是明白,也听得明白,自己一写就一塌糊涂。哎,还是多练吧。。


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
Author's latest blog post