简单js做计算器

2019-06-13  本文已影响0人  Python黑户

计算器

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>switch练习2</title>
        <script type="text/javascript">
            window.onload = function(){
                var js = document.getElementById('js')
                js.onclick = function(){
                    while (true){
                        var in1 = document.getElementById('in1').value;
                        var in2 = document.getElementById('in2').value;
                        var st1 = document.getElementById('st1').value;
                        in1 = parseInt(in1);
                        in2 = parseInt(in2);
                        if (isNaN(in1) || isNaN(in2)){
                            alert('输入不合法');
                            break
                        }else{
                            if (st1 == 'a'){
                                var b = in1+in2;
                                alert(in1+'+'+in2+'='+b);
                                break
                            }else if (st1 == 'b'){
                                var b = in1-in2;
                                alert(in1+'-'+in2+'='+b);
                                break
                            }else if (st1 == 'c'){
                                var b = in1*in2;
                                alert(in1+'*'+in2+'='+b);
                                break
                            }else if (st1 == 'd'){
                                var b = in1/in2;
                                alert(in1+'/'+in2+'='+b);
                                break
                            }
                        }
                    }
                }
            }
        </script>
    </head>
    <body>
        <input type="text" name="" id="in1">
        <select id="st1">
            <option value="a">+</option>
            <option value="b">-</option>
            <option value="c">*</option>
            <option value="d">/</option>
        </select>
        <input type="text" name="" id="in2">
        <input type="button" name="" value="计算" id="js">
    </body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读