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
What is DNS The full name of DNS is Domain Name S...
1. MySQL gets the current date and time function ...
Table of contents How to deploy MySQL service usi...
We can view the installation path of mysql throug...
I have been in contact with MGR for some time. Wi...
html ¶ <html></html> html:xml ¶ <h...
Preface: Docker port mapping is often done by map...
Introduction to MySQL Window Functions MySQL has ...
There is often a lack of understanding of multi-c...
Table of contents 1. Synchronous AJAX 2. Asynchro...
Overview Today I will mainly share how to correct...
Flex Layout Flex is the abbreviation of Flexible ...
Function: Jump to the previous page or the next p...
This article example shares the specific code for...
2048 mini game, for your reference, the specific ...