The final effect is as shown in Figure 2. There is a bug: the result of clicking % after integer is correct. If % is clicked after decimal, the result will be wrong! The rest are normal, please give me some advice: the value of input is of string type. How to correctly handle the if part in Figure-1 below in JS? ?
Figure-1
Figure-2
The HTML code is as follows
<body><div id="calculator"> <div> <span>Simple Calculator</span> <span>@walker</span> </div> <div id="shuRu"> <!--screen input bar--> <div> <input type="text" id="screenName" name="screenName"> </div> </div> <div id="keys"> <!-- j --> <!--first row--> <input type="button" id="7" onclick="jsq(this.id)" value="7"> <input type="button" id="8" onclick="jsq(this.id)" value="8"> <input type="button" id="9" onclick="jsq(this.id)" value="9"> <input type="button" id="Back" onclick="tuiGe()" value="Back"> <input type="button" id="C" onclick="clearNum()" value="C" style="margin-right:0px"> <!--Second row--> <input type="button" id="4" onclick="jsq(this.id)" value="4"> <input type="button" id="5" onclick="jsq(this.id)" value="5"> <input type="button" id="6" onclick="jsq(this.id)" value="6"> <input type="button" id="*" onclick="jsq(this.id)" value="X"> <input type="button" id="/" onclick="jsq(this.id)" value="/" style="margin-right:0px"> <!--Third row--> <input type="button" id="1" onclick="jsq(this.id)" value="1"> <input type="button" id="2" onclick="jsq(this.id)" value="2"> <input type="button" id="3" onclick="jsq(this.id)" value="3"> <input type="button" id="+" onclick="jsq(this.id)" value="+"> <input type="button" id="-" onclick="jsq(this.id)" value="-" style="margin-right:0px"> <!--Fourth row--> <input type="button" id="0" onclick="jsq(this.id)" value="0"> <input type="button" id="00" onclick="jsq(this.id)" value="00"> <input type="button" id="." onclick="jsq(this.id)" value="."> <input type="button" id="%" onclick="jsq(this.id)" value="%"> <input type="button" id="eva" onclick="eva()" value="=" style="margin-right:0px"> </div> <div> <span>Welcome to JavaScript calculator</span> <span> <a href="#" target="_blank">Feedback</a> </span> </div></div></body>
The CSS code is as follows:
<style> /*Basic reset*/*{ margin:0; padding:0; box-sizing: border-box; font: 14px Arial,sans-serif;}html{ height:100%; background-color:lightslategrey;}#calculator{ margin: 15px auto; width:330px; height:400px; border: 1px solid lightgray; background-color:darkgrey; padding:15px;}/*LOGO*/.LOGO{ height:20px;}.LOGO .name{ float:left; line-height:30px;}.LOGO .verson{ float:right; line-height:30px;}/*screen*/#shuRu{ margin-top:15px;}.screen{ margin-top:5px; width:300px; height:40px; text-align: right; padding-right:10px; font-size:20px;}#keys{ border:1px solid lightgray; height:223px; margin-top:25px; padding:8px;}#keys .last{ margin-right:0px;}.footer{ margin-top:20px; height:20px;}.footer .link{ float:right;}#keys .buttons{ float:left; width: 42px; height: 36px; text-align:center; background-color:lightgray; margin: 0 17px 20px 0;} </style>The javascript code is as follows:
<script> var num = 0; // Define the data of the first input function jsq(num) { //Get the current input if(num=="%"){ document.getElementById('screenName').value=Math.round(document.getElementById('screenName').value)/100; }else{ document.getElementById('screenName').value += document.getElementById(num).value; } } function eva() { //Calculate the input result document.getElementById("screenName").value = eval(document.getElementById("screenName").value); } function clearNum() { //Clear 0 document.getElementById("screenName").value = null; document.getElementById("screenName").focus(); } function tuiGe() { //Backspace var arr = document.getElementById("screenName"); arr.value = arr.value.substring(0, arr.value.length - 1); } </script>The above article javascript implements simple calculator effect [Recommended] is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.