基于javascript的简易计算器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35219609/article/details/78013896
<script>
        var num = 0;  // 定义第一个输入的数据
        function jsq(num) {
            //获取当前输入
            if(num=="%"){
                document.getElementById('screenName').value=Math.round(document.getElementById('screenName').value)/100;
            }else{
                document.getElementById('screenName').value += document.getElementById(num).value;
            }
        }
        function eva() {
            //计算输入结果
            document.getElementById("screenName").value = eval(document.getElementById("screenName").value);
        }
        function clearNum() {
            //清0
            document.getElementById("screenName").value = null;
            document.getElementById("screenName").focus();
        }
        function tuiGe() {
            //退格
            var arr = document.getElementById("screenName");
            arr.value = arr.value.substring(0, arr.value.length - 1);
        }
    </script>

<body>
 <center>
<div id="calculator">
    <div id="shuRu">
        <div>
            <input type="text" id="screenName" name="screenName" style="width:233;">
        </div>
    </div>
    <div id="keys">
        <div>
        <input type="button" id="7" onclick="jsq(this.id)" value="7" style="width:40;">
        <input type="button" id="8" onclick="jsq(this.id)" value="8"  style="width:40;">
        <input type="button" id="9" onclick="jsq(this.id)" value="9" style="width:40;">
        <input type="button" id="Back" onclick="tuiGe()" value="Back" style="width:40;">
        <input type="button" id="C" onclick="clearNum()" value="C" style="width:40;">
        </div>
        <div>
        <input type="button" id="4" onclick="jsq(this.id)" value="4" style="width:40;">
        <input type="button" id="5" onclick="jsq(this.id)" value="5" style="width:40;">
        <input type="button" id="6" onclick="jsq(this.id)" value="6" style="width:40;">
        <input type="button" id="*" onclick="jsq(this.id)" value="*" style="width:40;">
        <input type="button" id="/" onclick="jsq(this.id)" value="/" style="width:40;">
        </div>
        <div>
        <input type="button" id="1" onclick="jsq(this.id)" value="1" style="width:40;">
        <input type="button" id="2" onclick="jsq(this.id)" value="2" style="width:40;">
        <input type="button" id="3" onclick="jsq(this.id)" value="3" style="width:40;">
        <input type="button" id="+" onclick="jsq(this.id)" value="+" style="width:40;">
        <input type="button" id="-" onclick="jsq(this.id)" value="-" style="width:40;">
        </div>
        <div>
        <input type="button" id="0" onclick="jsq(this.id)" value="0" style="width:40;">
        <input type="button" id="00" onclick="jsq(this.id)" value="00" style="width:40;">
        <input type="button" id="." onclick="jsq(this.id)" value="." style="width:40;">
        <input type="button" id="%" onclick="jsq(this.id)" value="%" style="width:40;">
        <input type="button" id="eva" onclick="eva()" value="=" class="buttons" style="width:40;">
        </div>
    </div>
</div>
</center>
</body>

猜你喜欢

转载自blog.csdn.net/qq_35219609/article/details/78013896
今日推荐