This article example shares the specific code of JavaScript to implement a simple calculator function for your reference. The specific content is as follows The specific requirements are as follows: Implementation code: <html> <head> <meta charset="utf-8"> <title>Calculator</title> <script> function myck(type){ var num1 = document.getElementById("num1"); var num2 = document.getElementById("num2"); if(type==1){ // Calculation operation var result = parseInt(num1.value) + parseInt(num2.value); alert(result); document.getElementById("resultDiv").innerText = "Final calculation result: " + result; }else if(type==2){ var result = parseInt(num1.value) - parseInt(num2.value); alert(result); document.getElementById("resultDiv").innerText = "Final calculation result: " + result; } else if(type==3){ var result = parseInt(num1.value) * parseInt(num2.value); alert(result); document.getElementById("resultDiv").innerText = "Final calculation result: " + result; } else if(type==4){ if(confirm("Is it cleared correctly?")){ // Clear num1.value = ""; num2.value = ""; document.getElementById("resultDiv").innerText=""; } } } </script> </head> <body> <div style="margin-top: 100px;margin-left: 500px;"> <span style="font-size: 60px;">Calculator</span> </div> <div> <div class="innerDiv" style="margin-left: 490px;"> Number 1: <input id="num1" type="number" placeholder="Please enter number 1"> </div> </div> <div> <div class="innerDiv" style="margin-left:490px;"> Number 2: <input id="num2" type="number" placeholder="Please enter number 2"> </div> </div> <div> <div style="margin-left: 500px;" class="innerDiv"> <input type="button" onclick="myck(1)" value="Add"> <input type="button" onclick="myck(2)" value="Subtract"> <input type="button" onclick="myck(3)" value="Multiply"> <input type="button" onclick="myck(4)" value="Clear Empty"> </div> </div> <div id="resultDiv"> </div> </body> <style> .innerDiv{ margin-left: 420px; margin-top: 20px; } </style> </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:
|
<<: How to use nginx to simulate blue-green deployment
>>: How to solve the problem that MySQL cannot start because it cannot create PID
1. <select style="width:195px" name=&...
Table of contents 1. filter() 2. forEach() 3. som...
The parent node of the parent node, for example, t...
Data is the core asset of an enterprise and one o...
1. Get the image #Specify the version that includ...
1. Download the required packages wget -P /usr/lo...
Let's first look at some simple data: Accordin...
Prerequisite: The web developer plugin has been in...
When encapsulating the date picker, you need to d...
Table of contents 1 Difference 1.1 Space Occupanc...
As one of the most popular front-end frameworks, ...
Table of contents 1. Basic event handling 2. Send...
1. Embed is illegal The <embed> tag is a pri...
The first one: Using the CSS position property &l...
1. I purchased a VPS and CentOS system, and found...