This article shares the specific code of JavaScript simulation calculator for your reference. The specific content is as follows Function: 1. Click the button to enter the number Knowledge points used: 1. Use a large number of custom functions to implement business logic Purpose of comprehensive exercises: 1. Effectively combine CSS, HTML and JS to realize business functions And I just started to learn js recently, and I find it very interesting. I was not that interested when I was learning the basics of java. I feel that js is very interesting when I first start using it. Here is a simple calculator source code: html page: <!DOCTYPE html> <html> <head> <title>js calculator</title> <link rel="stylesheet" type="text/css" href="index.css" > <script type="text/javascript" src="index.js"> </script> </head> <body onload="init()"> <!-- 1 text box 10 numbers .... 20 buttons --> <div id="div1"> <form action=""> <div id="div2"> <input type="text" name="num" disabled="disabled" id="num" value="0"> </div> </form> <div id="div3"> <input type="button" name="" value="C" id="baidu"> <input type="button" name="" value="←" id=""> <input type="button" name="" value="+/-" id=""> <input type="button" name="" value="/" id=""> <input type="button" name="" value="7" id=""> <input type="button" name="" value="8" id=""> <input type="button" name="" value="9" id=""> <input type="button" name="" value="*" id=""> <input type="button" name="" value="4" id=""> <input type="button" name="" value="5" id=""> <input type="button" name="" value="6" id=""> <input type="button" name="" value="-" id=""> <input type="button" name="" value="1" id="" > <input type="button" name="" value="2" id="" > <input type="button" name="" value="3" id="" > <input type="button" name="" value="+" id=""> <input type="button" name="" value="0" id=""> <input type="button" name="" value="=" id=""> <input type="button" name="" value="." id=""> <input type="button" name="" value="AC" id=""> </div> </div> </body> </html> js page: function init(){ var num = document.getElementById("num"); num.value=0; var btn_num1; var fh; num.disabled="disabled"; // var n1=document.getElementById("n1"); //n1.onclick=function(){ // } var oButton = document.getElementsByTagName("input"); for(var i=0;i<oButton.length;i++){ oButton[i].onclick=function(){ if (isnumber (this.value)) { //num.value=(num.value+this.value)*1; //Eliminate the default 0 if(isNull(num.value)){ num.value=this.value; }else{ num.value=num.value+this.value; } }else{ //Test whether the function is correct// alert("bushishuzi") var btn_num=this.value; //Test whether the function is correct (pop-up window) // alert(btn_num); switch(btn_num){ case "+": // alert(11); btn_num1=num.value*1; //=parseInt(num.value) This is also OK, the following words need to be changed to number num.value=0; fh="+"; break; case "-": btn_num1=num.value*1; num.value=0; fh="-"; break; case "*": btn_num1=num.value*1; num.value=0; fh="*"; break; case "/": btn_num1=num.value*1; num.value=0; fh="/"; break; case ".": num.value=dec_number(num.value); break; case "←": num.value=back(num.value); break; case "+/-": num.value=sign(num.value); break; case "AC": num.value="0"; break; case "C": init_baidu(); break; case "=": switch(fh){ case"+": num.value=btn_num1+num.value*1; break; case"-": num.value=btn_num1-num.value*1; break; case"*": num.value=btn_num1*num.value*1; break; case"/": if(num.value==0){ num.value=0; alert("The divisor cannot be 0"); }else{ num.value=btn_num1/num.value*1; } break; } break; } } } } } //Decimal point function function dec_number(n){ if(n.indexOf(".")==-1){ n=n+"."; } return n; } //Verify that the text box is empty or 0 function isNull(n){ if(n*1==0||n.length==0){ return true; }else{ return false; } } //Back key function back(n){ n=n.substr(0,n.length-1); if (isNull(n)) { n="0"; } return n; } //Positive and negative signs +/- function sign(n){ if(n.indexOf("-")==-1){ n="-"+n; }else{ n=n.substr(1,n.length); } return n; } //isNaN: cannot be converted into a number: true, can be converted into a number: false function isnumber(n){ return !isNaN(n); } //Button C uses a hyperlink to Baidu, which can be used at will. function init_baidu(){ window.location.href="http://www.baidu.com"; } CSS page: *{ margin:0px; padding:0px; } div{ width:170px; } #div1{ top:60px; left: 100px; position:absolute; } input[type="button"]{ width:30px; margin-right: 5px; } input[type="text"]{ width:147px; text-align: right; background-color:white; border:1px solid; padding-right:1px; box-sizing:content-box; } input[type="button"]:hover{/*//Use of pseudo-class and button*/ background-color:white; border:1px solid; } 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:
|
<<: Ubuntu basic settings: installation and use of openssh-server
>>: MySQL 5.7.18 version free installation configuration tutorial
What is the purpose of this sentence? Copy code Th...
Today, when I was writing a small program, I used...
Table of contents 1. for loop 2. Double for loop ...
Seurat is a heavyweight R package for single-cell...
I recently started learning about database knowle...
Web front-end optimization best practices: conten...
Traditionally, developers create properties in Ja...
This article mainly introduces the layout method ...
Table of contents 1. Introduction 2. Basic Concep...
Related reading: Solve the problem that the servi...
Skip the Docker installation steps 1. Pull the po...
The browser displays TIF format images Copy code T...
Here is a Vue single sign-on demo for your refere...
First: First, confirm whether the server hardware ...
Every website usually encounters many non-search ...