This article shares the specific code of JavaScript to implement a simple calculator for your reference. The specific content is as follows <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Calculator</title> <style> .op { margin: 50px auto; width: 300px; height: 300px; border: 1px solid orange; background-color: lightskyblue; border-radius: 50px; } input { width: 210px; height: 30px; border-radius: 5px; } button { width: 30px; height: 30px; border-radius: 10px; } </style> </head> <body> <div class="op"> <h2 align="center">Calculator</h2> <form name="calculator"> <table align="center"> <tr> <td>num1:</td> <td><input type="text" name="num1"></td> </tr> <tr> <td>num2:</td> <td><input type="text" name="num2"></td> </tr> <tr> <td colspan="2">          <button type="button">+</button>        <button type="button">-</button>        <button type="button">*</button>        <button type="button">/</button> </td> </tr> <tr> <td>Results:</td> <td><input type="text" name="result" disabled></td> </tr> </table> </form> </div> <script> //Get all element objects under the tag name and return an array let buttonArr = document.getElementsByTagName("button"); for (let but of buttonArr) { but.addEventListener('click', function () { let operator = this.innerHTML; count(operator); }); } function count(operator) { //Get the input value of the form calculator named num1 let num1 = parseFloat(document.calculator.num1.value); let num2 = parseFloat(document.calculator.num2.value); let result = ''; switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if (num2 == 0) { alert("The divisor cannot be zero"); return; } result = num1 / num2; break; } //Assign the result to the input box with the attribute name result document.calculator.result.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:
|
<<: Explanation of the configuration and use of MySQL storage engine InnoDB
>>: A brief understanding of several scheduling algorithms for Nginx seven-layer load balancing
When using SQL to extract data, we often encounte...
Table of contents Select Structure Loop Structure...
1. Composition and related concepts of MySQL data...
1. Conclusion Syntax: limit offset, rows Conclusi...
Software and hardware environment centos7.6.1810 ...
letter-spacing property : Increase or decrease th...
Mongodb has a db.serverStatus() command, which ca...
First, the structure inside the nginx container: ...
Table of contents uni-app Introduction HTML part ...
Due to the limitation of CPU permissions, communi...
CEP - Complex Event Processing. The payment has n...
Preface Reduce is one of the new conventional arr...
Recently, I have been working on thesis proposals ...
The key features of the InnoDB storage engine inc...
Version Chain In InnoDB engine tables, there are ...