Blogger Information
Blog 61
fans 1
comment 0
visits 69731
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0510-一个小型计算器
我的博客
Original
725 people have browsed it

实例

<!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>
<!-- 1.获取页面元素的方法; 2.条件判断语句的使用方法 3.事件方法函数的作用 -->
<div class="box">
    <h2>小学生计算器</h2>
    <form>
        <table>
            <tr>
                <td><input type="text" name="num1" placeholder="数字1" autofocus></td>
                <td>
                    <select name="option">
                        <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="num2" 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 type="text/javascript">
    // 第一步  获取操作数1 name=num1,获取操作数2 name=num2 ,计算结果显示ID=result
    //1、后边跟着[0]或者item[0],标识获取到的是各个元素数组的第一个元素,默认是获取到一个数组。
    var num1 = document.getElementsByName('num1')[0];
    var num2 = document.getElementsByName('num2').item(0);
    var opt = document.getElementsByName('option')[0];
    var but = document.getElementsByTagName('button')[0];
    var res = document.getElementById('result');


   but.onclick=function(){
       var c1=0;
       var c2=0;   //先让c1 , c2 =0,个人觉得没意义。
       //判断num1和num2是不是数字
       if(num1.value.length===0){
           alert('数字1输入不能为空');
           num1.focus();
           return false;
       }
       else if(isNaN(num1.value)){
           alert('数字1输入不是数字');
           num1.focus();
           return false;
       }
       else if(num2.value.length===0){
           alert('数字2输入不能为空');
           num2.focus();
           return false;
       }
       else if(isNaN(num2.value)){
           alert('数字2输入不数字');
           num2.focus();
           return false;
       }
        else{
           //数据转换,把不是数字的 字符串转换为数字parseFloat
           //到这一步,两个数字不是空值,也是纯数字
           c1 = parseFloat(num1.value);
           c2 = parseFloat(num2.value);   //
           // 放在else外边就跟判断数值类型无关了,更没意义。
       }



       // 第二步 获取3个计算必须的数值和运算符
       var opt1  = opt.value;
       var c3 = 0 ;


    // 第三步 计算过程
       switch (opt1) {
          case 'add':
            c3 = c1 + c2;
            break;
          case 'sub':
            // flag='-';
            c3 = c1 - c2;
            break;
          case 'mul':
            // flag='*';
            c3 = c1 * c2;
            break;
          case 'div':
            // flag='/';
              if(c2===0){
                  alert('除数不能为零!');
            return  false;}
              else{
                  c3 = c1 / c2;
                  //保留2为小数,这个办法很好。保留3为小数就先*1000 在除1000.
                  c3 = Math.round(c3 * 100) / 100;
              }
    }


//如果上边不保留2位小数,1/2就等于0,这里判断就出错。
//     if (c3===0){
//         res.innerHTML = '看看哪里输错了';
//     }
//     else{
    res.innerHTML = c3;
    // }
   }



</script>


</body>
</html>

运行实例 »

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


Correction status:Uncorrected

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