博客列表 >小小计算器--2019-05-10

小小计算器--2019-05-10

小人物的博客
小人物的博客原创
2019年05月12日 23:06:48473浏览

实例

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

        table {
            margin: auto;
            /*参考线*/
            /*border: 1px solid red; */
        }

        td {
            width: 100px;
            height: 30px;
            padding: 5px 10px;
        }

        input, select {
            width: 100%;
            height:100%;
            border:none;
            text-align: left;
            padding-left: 15px;
            /*background-color: cyan;*/
        }

        button {
            width: 100%;
            height: 100%;
            border: none;
            background-color: skyblue;
            color: white;
        }
        button:hover {
            cursor: pointer;
            background-color: coral;
            width: 105%;
            height: 105%;
        }

    </style>
</head>
<body>
    <div class="box">
        <h2>小小计算器</h2>
        <form action="">
            <table>
                <tr>
                    <td><input type="text" name="opt1" placeholder="操作数1" autofocus></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 colspan="2" align="right"><h3>结果:</h3></td>
                    <td colspan="2" align="left"><h3 id="result"></h3></td>
                </tr>
            </table>
        </form>
    </div>
    <script>
        //获以操作数名
        var opt  = document.getElementsByName('option')[0];
        var opt1 = document.getElementsByName('opt1')[0];
        var opt2 = document.getElementsByName('opt2')[0];

        var btn = document.getElementsByTagName('button')[0];
        var result = document.getElementById('result');
        //给按钮添加事件
        btn.onclick = function (){
            var data1 = 0;
            var data2 = 0;

            if (opt1.value.length === 0) {
                alert('请输入操作数1!');
                opt1.focus();
                return false;
            } else if (isNaN(opt1.value)) {
                alert('操作数1必须为数字!');
                opt1.focus();
                return false;
            } else if (opt2.value.length === 0) {
                alert('请输入操作数2!');
                opt2.focus();
                return false;
            } else if (isNaN(opt2.value)) {
                alert('操作数2必须为数字!');
                return false;
            } else {
            data1 = parseFloat(opt1.value);
            data2 = parseFloat(opt2.value);
            }
        var option = opt.value;
        var temp = 0;
        var flag = '';
        switch (option) {
            case 'null':
                alert("请选择运算符");
                opt.focus();
                return false;
            case 'add':
                flag = '+';
                temp = data1 + data2;
                break;
            case 'sub':
                flag = '-';
                temp = data1 - data2;
                break;
            case 'mul':
                flag = '*';
                temp = data1 * data2;
                break;
            case 'div':
                flag = '/';
                if (data2 === 0 ) {
                    alert('除数不能为0,请重输!');
                    opt2.focus();
                    return false;
                } else {
                    temp = data1 / data2;
                    temp = math.round(temp * 100) /100;
                }
                break;
        }
        var str = '<span style="color:coral">';
        str += data1 +' '+flag+' ' +data2 + ' = ' + temp;
        str += '</span>';
        result.innerHTML = str;

        }

    </script>

</body>
</html>

运行实例 »

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


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议