How to implement calculator function using javascript
How to use javascript to implement the calculator function: [function init(){ var num=document.getElementById("num"); num.value=0; var btn_num1; var fh; ...].
#The operating environment of this article: windows10 system, javascript 1.8.5, thinkpad t480 computer.
Presumably everyone has written calculator functions in the process of learning programming languages, such as using php, java, python and other languages. So have you ever used javascript to implement the calculator function? In fact, the principles are the same, let’s take a look together!
HTML code:
<!DOCTYPE html> <html> <head> <title>js计算器</title> <link rel="stylesheet" type="text/css"href="index.css"> <script type="text/javascript" src="index.js"> </script> </head> <body onload="init()"> <!-- 1个文本框10个数字....20个按钮 --> <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 code:
function init(){ var num=document.getElementById("num"); num.value=0; var btn_num1; var fh; num.disabled="disabled"; // var n1=document.getElementById("n1"); // n1.οnclick=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;//把默认0消除 if(isNull(num.value)){ num.value=this.value; }else{ num.value=num.value+this.value; } }else{ //测试功能是否正确 // alert("bushishuzi") var btn_num=this.value; //测试功能是否正确(弹窗) // alert(btn_num); switch(btn_num){ case "+": // alert(11); btn_num1=num.value*1;//=parseInt(num.value)这个也可以,后面的话需要改为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("除数不能为0"); }else{ num.value=btn_num1/num.value*1; } break; } break; } } } } } //小数点的功能 function dec_number(n){ if(n.indexOf(".")==-1){ n=n+"."; } return n; } //验证文本框是否为空或者为0 function isNull(n){ if(n*1==0||n.length==0){ return true; }else{ return false; } } //退位键 function back(n){ n=n.substr(0,n.length-1); if(isNull(n)){ n="0"; } return n; } //正负号+/- function sign(n){ if(n.indexOf("-")==-1){ n="-"+n; }else{ n=n.substr(1,n.length); } return n; } //isNaN:不能转换成数字:true,可以转换成数字是false function isnumber(n){ return !isNaN(n); } //C按钮使用一个超级链接,链接到百度,这个可以随便发挥 function init_baidu(){ window.location.href="http://www.baidu.com"; }
CSS code:
*{ 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{/*//伪类和按钮的使用*/ background-color:white; border:1px solid; }
Recommended learning: javascript video tutorial
The above is the detailed content of How to implement calculator function using javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Efficient Fibonacci sequence calculator: PHP implementation of Fibonacci sequence is a very classic mathematical problem. The rule is that each number is equal to the sum of the previous two numbers, that is, F(n)=F(n -1)+F(n-2), where F(0)=0 and F(1)=1. When calculating the Fibonacci sequence, it can be implemented recursively, but performance problems will occur as the value increases. Therefore, this article will introduce how to write an efficient Fibonacci using PHP

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
