This article example shares the specific code of javascript to implement a simple calculator for your reference. The specific content is as follows Design a simple calculator Code <body> <a>First number</a> <input type="test" id="inputId1" value="" /><br/> <a>Second number</a> <input type="test" id="inputId2" value="" /><br/> <button onclick="cal('+')">+</button> <button onclick="cal('-')">-</button> <button onclick="cal('*')">*</button> <button onclick="cal('/')">/</button><br/> Calculation results <input type="test" id="resultId" value="" /> <script type="text/javascript"> // function add() { // console.log('add'); // var inputObj1 = document.getElementById('inputId1'); // var inputObj2 = document.getElementById('inputId2'); // var result = parseInt(inputObj1.value) + parseInt(inputObj2.value); // var resultObj = document.getElementById('result'); // resultObj.value = result; // console.log(result); // } function cal(type) { var inputObj1 = document.getElementById('inputId1'); var inputObj2 = document.getElementById('inputId2'); switch(type){ case '+': var result = parseInt(inputObj1.value) + parseInt(inputObj2.value); break; case '-': var result = parseInt(inputObj1.value) - parseInt(inputObj2.value); break; case '*': var result = parseInt(inputObj1.value) * parseInt(inputObj2.value); break; case '/': var result = parseInt(inputObj1.value) / parseInt(inputObj2.value); break; } var resultObj = document.getElementById('resultId'); resultObj.value = result; } </script> </body> </html> 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 basic SQL statements in MySQL database
>>: How to run Python script on Docker
This is to commemorate the 4 pitfalls I stepped o...
Environment: CentOS 7 Official documentation: htt...
Table of contents JavaScript private class fields...
1. Achieve the effect 2 Knowledge Points 2.1 <...
When position is absolute, the percentage of its ...
Table of contents Preface && Operator || ...
View installation and uninstallation # View rpm -...
1. MyISAM storage engine shortcoming: No support ...
Table of contents Tutorial Series 1. User Managem...
Table of contents 1. Anti-shake 2. Throttling 3. ...
Table of contents 1. Variable Overview 1.1 Storag...
Set Anchor Point <a name="top"><...
Start the centos8 virtual machine and press the u...
Problem Description 1. Database of the collection...
This article shares the MySQL installation and co...