Use JS to complete a simple calculator for your reference. The specific contents are as follows Requirements: The input value can only be a number, use a regular expression <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Calculator</title> </head> <body> <div style="text-align: center;"> <input type="text" id="1" onkeyup="this.value=this.value.replace(/[^\d]/g,'') " > <select name="select" id="select"> <option value="null">Please select</option> <option value="0">+</option> <option value="1">-</option> <option value="2">*</option> <option value="3">/</option> </select> <input type="text" id="2" onkeyup="this.value=this.value.replace(/[^\d]/g,'') "> = <input type="text" id="3" onkeyup="this.value=this.value.replace(/[^\d]/g,'') "><br><br> </div> <div style="text-align: center;"> <input type="button" id="button"value="calculate"> </div> <script> function accAdd(arg1,arg2){ var r1,r2,m; try{ r1=arg1.toString().split(".")[1].length }catch(e){ r1=0 } try{ r2=arg2.toString().split(".")[1].length }catch(e){ r2=0 } m=Math.pow(10,Math.max(r1,r2)) return (arg1*m+arg2*m)/m } Number.prototype.add = function (arg) { return accAdd(arg,this); } function Subtr(arg1,arg2){ var r1,r2,m,n; try{ r1=arg1.toString().split(".")[1].length }catch(e){ r1=0 } try{ r2=arg2.toString().split(".")[1].length }catch(e){ r2=0 } m=Math.pow(10,Math.max(r1,r2)); // last modified by deeka // Dynamically control the precision length n=(r1>=r2)?r1:r2; return ((arg1*m-arg2*m)/m).toFixed(n); } function accMul(arg1,arg2) //multiplication { var m=0,s1=arg1.toString(),s2=arg2.toString(); try{ m+=s1.split(".")[1].length }catch(e){} try{ m+=s2.split(".")[1].length }catch(e){} return Number(s1.replace(".",""))*Number(s2.replace(".","")) / Math.pow(10,m) } //Add a mul method to the Number type to make it more convenient to call. Number.prototype.mul = function (arg) { return accMul(arg, this); } function accDiv(arg1,arg2){ var t1=0,t2=0,r1,r2; try{ t1=arg1.toString().split(".")[1].length }catch(e){} try{ t2=arg2.toString().split(".")[1].length }catch(e){} with(Math){ r1=Number(arg1.toString().replace(".","")) r2=Number(arg2.toString().replace(".","")) return (r1/r2)*pow(10,t2-t1); } } Number.prototype.div = function (arg) { return accDiv(this, arg); } document.getElementById('button').onclick=function(){ var num1 = document.getElementById('1').value; var num2 = document.getElementById('2').value; var num3; var op = document.getElementById('select').value; switch(op){ case '0': num3 = accAdd(num1,num2); break; case '1': num3 = Subtr(num1,num2); break; case '2': num3=accMul(num1,num2); break; case '3': num3=accDiv(num1,num2); break; } document.getElementById('3').value=num3; } </script> </body> </html> Effect picture: 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:
|
<<: Summary of MySQL common SQL statements including complex SQL queries
>>: Zabbix monitors the process of Linux system services
Example: We use the Python code loop_hello.py as ...
In front-end projects, attachment uploading is a ...
<br />For each of our topics, the team will ...
What is Docker-Compose The Compose project origin...
If your DOCTYPE is as follows: Copy code The code ...
What to do if you forget Windows Server 2008R2 So...
This article example shares the specific code of ...
Copy code The code is as follows: <HTML> &l...
1For example: To split the fields shown in Figure...
Table of contents Overview first step Step 2 Why ...
Table of contents docker system df docker system ...
1. How to install? 1. [Run] -> [cmd] to open t...
Table of contents background Why error handling? ...
Table of contents Date Object Creating a Date Obj...
First of all, this post is dedicated to Docker no...