码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript简易计算器

时间:2016-03-05 22:00:44      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

技术分享

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS计算器</title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body style="margin:50px">
    <div class="form-group">
        <div class="col-sm-3">
            <input type="text" id="sum1" class="form-control" placeholder="请输入第一个数">
        </div>
        <div class="col-sm-2">
            <select id="sign" class="form-control">
            <option></option>
            <option></option>
            <option></option>
            <option></option>
            <option>取余</option>
        </select>
        </div>
        <div class="col-sm-3">
            <input type="text" id="sum2" placeholder="请输入第二个数" class="form-control">
        </div>
        <div class="col-sm-1">
            <button id="equal" class="btn btn-info" style="width:100%;" onclick="equals()">=</button>
        </div>
        <div class="col-sm-3">
            <label id="count"class="form-control"></label>
        </div>
    </div>

    <script type="text/javascript">
        var sum1;
        var sum2;
        var sign;
        var fruit;
        function equals(){
            sum1 = parseInt(document.getElementById("sum1").value);
            sum2 = parseInt(document.getElementById("sum2").value);
            if (isNaN(sum1) || isNaN(sum2)) {
                document.getElementById("count").innerHTML = "您输入的不是数字,请重新输入!";
            }else{
                sign = document.getElementById("sign").value;

                switch(sign){
                    case :
                        fruit = sum1 + sum2;
                        break;
                    case :
                        fruit = sum1 - sum2;
                        break;
                    case :
                        fruit = sum1 * sum2;
                        break;
                    case :
                        if (sum2 == 0) {
                            fruit = "被除数不能为0,请重新输入!";
                            break;
                        }else{
                            fruit = sum1 / sum2;
                            break;
                        }
                    case 取余:
                        fruit = sum1 % sum2;
                        break;
                }
                document.getElementById("count").innerHTML = fruit;
                };
            }
    </script>
</body>
</html>

 

JavaScript简易计算器

标签:

原文地址:http://www.cnblogs.com/Harold-Hua/p/5245907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!