This article example shares the specific code of js to implement a simple calculator for your reference. The specific content is as follows Recently, the blogger was bored and wrote a simple calculator using js (the blogger is working on the backend, not the frontend >_<). In fact, I feel very happy working on the front-end. It is very fulfilling to create something by myself. Without further ado, here is the source code. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Calculator</title> <style> #b1{ text-align: center; background-color: lightblue; width: 650px; height: 650px; border: 1px solid black; margin-left: 400px; border-radius: 18px; } #b2{ text-align: center; width: 300px; height: 80px; margin-top: 30px; font-family: "Microsoft YaHei"; font-size: 20px; border-radius: 8px; outline:none; } ul{ list-style-type: none; } li{ float: left; margin-left: 40px; margin-top: 30px; } li input{ width: 100px; height: 50px; border-radius: 8px; font-family: "Microsoft YaHei"; font-size: 20px; outline:none; } li input:hover{ background-color:red ; } </style> <script> var beforeNum=0; function addNum(a){ beforeNum=document.getElementById("b2").value; var nowNum=beforeNum; if(beforeNum!=0){ nowNum=beforeNum+a; }else{ if(beforeNum.substring(beforeNum.length-2,beforeNum.length-1)=="."){ nowNum=beforeNum+a; } if(a!="+"&&a!="-"&&a!="*"&&a!="/"&&(beforeNum.substring(beforeNum.length-2,beforeNum.length-1)!=".")){ nowNum=a; } } document.getElementById("b2").value=nowNum; } function jisuan(){ document.getElementById("b2").value=eval(document.getElementById("b2").value); } function clearNum(){ document.getElementById("b2").value=0; } function backAgain(){ beforeNum=document.getElementById("b2").value; if(beforeNum.length!=1){ document.getElementById("b2").value=beforeNum.substring(0,beforeNum.length-1); }else if(beforeNum.length==1){ document.getElementById("b2").value=0; } } function addPoint(b){ beforeNum=document.getElementById("b2").value; if((beforeNum.indexOf(".")!=beforeNum.length-1)){ document.getElementById("b2").value=beforeNum+b; } } function offCl(){ document.getElementById("b2").value=""; } </script> </head> <body> <div id="b1"> <p style="font-size: 20px;">Online simple calculator</p> <div><input id="b2" name="wenben" value="0"/></div> <div> <ul> <li><input type="button" value="1" onclick="addNum(this.value);"/></li> <li><input type="button" value="2" onclick="addNum(this.value);"/></li> <li><input type="button" value="3" onclick="addNum(this.value);"/></li> <li><input type="button" value="4" onclick="addNum(this.value);"/></li> </ul> <ul> <li><input type="button" value="5" onclick="addNum(this.value);"/></li> <li><input type="button" value="6" onclick="addNum(this.value);"/></li> <li><input type="button" value="7" onclick="addNum(this.value);"/></li> <li><input type="button" value="8" onclick="addNum(this.value);"/></li> </ul> <ul> <li><input type="button" value="9" onclick="addNum(this.value);"/></li> <li><input type="button" value="0" onclick="addNum(this.value);"/></li> <li><input type="button" value="+" onclick="addNum(this.value);"/></li> <li><input type="button" value="-" onclick="addNum(this.value);"/></li> </ul> <ul> <li><input type="button" value="*" onclick="addNum(this.value);"/></li> <li><input type="button" value="/" onclick="addNum(this.value);"/></li> <li><input type="button" value="." onclick="addPoint(this.value);"/></li> <li><input type="button" value="=" onclick="jisuan();"/></li> </ul> </ul> <ul> <li><input type="button" value="Back" onclick="backAgain();"/></li> <li><input type="button" value="Clear" onclick="clearNum();"/></li> <li><input type="button" value="Shutdown" onclick="offCl();"/></li> </ul> </div> </div> </body> </html> The effect diagram of code running. In addition, the blogger performed a series of algorithm judgments when writing js functions and screened out some non-standard algorithms. If you are interested, you can extend these algorithm verifications. If you have any ideas or suggestions, you can communicate with the blogger. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Solution to 1045 error in mysql database
>>: Solution to forgetting the root password of MySQL 5.7 and 8.0 database
Recently, during the development process, I encou...
Original article, please indicate the author and ...
This article example shares the specific code of ...
Optimistic Locking Optimistic locking is mostly i...
This article introduces 4 methods to achieve mask...
I recently used Docker to upgrade a project. I ha...
Table of contents Business requirements: Solution...
This article shares the installation and configur...
Table of contents Method 1: set: It is not a data...
Introduction: When using MySQL to create a table,...
Table of contents In JavaScript , there are sever...
This article shares the installation tutorial of ...
ant-design-vue customizes the use of Ali iconfont...
Centos7 startup process: 1.post(Power-On-Self-Tes...
7 ways to implement a two-column layout with fixe...