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
Achieve resultsImplementation Code html <div&g...
Table of contents Install Tomcat with Docker Use ...
1. First, you need to know what will trigger the v...
This article is based on Linux centos8 to install...
Problem Description When using Windows Server 201...
This article example shares the specific code of ...
Table of contents Create an image File Structure ...
Recently, the business side reported that some us...
Messy log Nginx in daily use is mostly used as bo...
Antd+react+webpack is often the standard combinat...
Last night I wrote an essay about the browser ren...
Table of contents What is JSONP JSONP Principle J...
Related Articles: Website Design for User Experien...
In the previous chapters, we have learned how to ...
Table of contents tool: Login scenario: practice:...