Home>Article>Web Front-end> Teach you a trick to implement a simple calculator
This article will give you a detailed introduction to the method of implementing a simple calculator in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Page layout design (HTML CSS)
due to the previous blog There is a detailed explanation of html and css. Again, I will not go into details and go directly to the code. Because the JQuery selector is used in js, JQuery is introduced in the html using the tag. In the html, the calculator event cal() is bound to each button click and the current click object is passed in. this.
.html file:
简单计算器
.css file:
input{ width: 200px; height:50px; margin-bottom: 10px; padding: 0; font:18px bold;}button{ width: 50px; height: 40px; margin-bottom: 10px; border: 1px dashed black; background-color: #ffc4cc;}#c24{ height: 93px;}#c44{ height: 93px;}#c51{ width: 122px;}#c44,#c24,#c14{ margin-left:10px;}
Static page as shown:
Implement the calculation part ( JS)
1. Function: realize simple addition, subtraction, multiplication and division calculations of numerical values, and clear screen function
2. Operation: For example: 123×29; click 1, 2, 3 ,, click the × sign, click 2, 9 in sequence, and finally click =, you can calculate the result 3567
. The example is shown in the figure:
3. Disadvantages:
4. Idea display:
The code is as follows:
var input = $("input");
The code is as follows:
let btn = e.id;
The code is as follows:
//若input的值为0 input.val(btn_value);//若input的值非0 input.val(input.val()+btn_value);
The code is as follows:
//若input的值含有+、×、/ alert("连续运算功能未上线!")//若input的值不含有+、×、/ input.val(input.val()+当前运算符号);
The code is as follows:
if(input_value.indexOf("+")!==-1){ pos = input_value.indexOf("+"); num1 = parseFloat(input_value.substring(0,pos)); num2 = parseFloat(input_value.substring(pos+1,input_value.length)); input.val(num1+num2); } else if(input_value.indexOf("-")!==-1){ pos = input_value.indexOf("-"); num1 = parseFloat(input_value.substring(0,pos)); num2 = parseFloat(input_value.substring(pos+1,input_value.length)); input.val(num1-num2); } else if(input_value.indexOf("×")!==-1){ pos = input_value.indexOf("×"); num1 = parseFloat(input_value.substring(0,pos)); num2 = parseFloat(input_value.substring(pos+1,input_value.length)); input.val(num1*num2); } else if(input_value.indexOf("/")!==-1){ pos = input_value.indexOf("/"); num1 = parseFloat(input_value.substring(0,pos)); num2 = parseFloat(input_value.substring(pos+1,input_value.length)); if(num2-0 === 0){ alert("分母不能为0!"); } else{ input.val(num1/num2); } }
The code is as follows:
input.val(0);
5. The JavaScript file is as follows:
"use strict"var input = $("input");function cal(e){ let btn = e.id; //清零 if( btn === "c24"){ input.val(0); } //数值输入 else if(btn === "c51"||btn === "c41"||btn === "c42"||btn === "c43" ||btn === "c31"||btn === "c32"||btn === "c33" ||btn === "c21"||btn === "c22"||btn === "c23"){ let btn_value = document.getElementById(btn).getAttribute("value"); if( input.val() === "0" ){ input.val(btn_value); } else{ input.val(input.val()+btn_value); } } else if(btn === "c11"){ let input_value = input.val(); if(input_value.indexOf("+") === -1&&input_value.indexOf("-") === -1 &&input_value.indexOf("×") === -1&&input_value.indexOf("/") === -1){ input.val(input.val()+"+"); } else{ alert("连续运算功能未上线!") } } else if(btn === "c12"){ let input_value = input.val(); if(input_value.indexOf("+") === -1&&input_value.indexOf("-") === -1 &&input_value.indexOf("×") === -1&&input_value.indexOf("/") === -1){ input.val(input.val()+"-"); } else{ alert("连续运算功能未上线!") } } else if(btn === "c13"){ let input_value = input.val(); if(input_value.indexOf("+") === -1&&input_value.indexOf("-") === -1 &&input_value.indexOf("×") === -1&&input_value.indexOf("/") === -1){ input.val(input.val()+"×"); } else{ alert("连续运算功能未上线!") } } else if(btn === "c14"){ let input_value = input.val(); if(input_value.indexOf("+") === -1&&input_value.indexOf("-") === -1 &&input_value.indexOf("×") === -1&&input_value.indexOf("/") === -1){ input.val(input.val()+"/"); } else{ alert("连续运算功能未上线!") } } else if(btn === "c53"){ input.val(input.val()+"."); } else if(btn === "c44"){ let pos,num1,num2; let input_value = input.val(); if(input_value.indexOf("+")!==-1){ pos = input_value.indexOf("+"); num1 = parseFloat(input_value.substring(0,pos)); num2 = parseFloat(input_value.substring(pos+1,input_value.length)); input.val(num1+num2); } else if(input_value.indexOf("-")!==-1){ pos = input_value.indexOf("-"); num1 = parseFloat(input_value.substring(0,pos)); num2 = parseFloat(input_value.substring(pos+1,input_value.length)); input.val(num1-num2); } else if(input_value.indexOf("×")!==-1){ pos = input_value.indexOf("×"); num1 = parseFloat(input_value.substring(0,pos)); num2 = parseFloat(input_value.substring(pos+1,input_value.length)); input.val(num1*num2); } else if(input_value.indexOf("/")!==-1){ pos = input_value.indexOf("/"); num1 = parseFloat(input_value.substring(0,pos)); num2 = parseFloat(input_value.substring(pos+1,input_value.length)); if(num2-0 === 0){ alert("分母不能为0!"); } else{ input.val(num1/num2); } } }}
[Recommended learning:javascript advanced tutorial】
The above is the detailed content of Teach you a trick to implement a simple calculator. For more information, please follow other related articles on the PHP Chinese website!