Blogger Information
Blog 16
fans 0
comment 0
visits 12552
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5.10计算器
如花似玉的小牛牛的博客
Original
752 people have browsed it

实例

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

<div>
    <input type="text">
    <select id="option"  >
        <option value="a">+</option>
        <option value="b">-</option>
        <option value="c">*</option>
        <option value="d">/</option>
    </select>
    <input type="text">
    <button>计算器</button>
    <hr>
    <li></li>
</div>
<script>

    var a1 = document.getElementsByTagName("input")[0];
    var a2 = document.getElementsByTagName("input")[1];
    var opt = document.getElementById("option");
    for (i=0; i<opt.length; i++);
    var button = document.getElementsByTagName("button")[0];
    button.onclick =function () {
        var data=0;
        var data1=0;
        if(a1.value.length===0 && a2.value.length===0){
            alert('不能为空');
            a1.focus();
            a2.foucs();
            return false;
        }else if ((isNaN(a1.value)) && (isNaN(a2.value))) {
            alert('只能为数字');
            a1.focus();
            a2.foucs();
            return false;

        }else {
            //转换数值
           data =parseFloat(a1.value);
           data1=parseFloat(a2.value);
        }

        var opction =opt.value;
        var zan=0;
        var falg ='';
        switch (opction) {
            case 'a':
                falg ='+';
                zan=data + data1;
                break;
            case 'b':
                falg ='-';
                zan=data - data1;
                break;
            case 'c':
                falg ='*';
                zan=data * data1;
                break;
            case 'd':
                falg ='/';
                if(data1 ===0){
                    alert('除数不能为0');
                    return falg;
                }else{
                    zan=data / data1;
                    zan =Math.round(zan *100) /100;
                }
                break;
        }
        var li =document.getElementsByTagName("li")[0];
        var p =document.createElement("p");
        p.innerText= data1 + falg +data1 + '=' + zan ;
        li.appendChild(p);
    }
</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