Using JavaScript to write simple calculator _javascript tips

Source: Internet
Author: User

This method is super simple, the idea is very worthy of recommendation, small partners to refer to the next bar

Copy Code code as follows:

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>javascript Simple Calculator </title>
<script>
Define global variable var one= "1";
var two= "2";
var three= "3";
var four= "4";
var five= "5";
var six= "6";
var seven= "7";
var eight= "8";
var nine= "9";
var zero= "0";
var plus= "+", minus= "-", multiply= "*", divide= "/", decimal= ".";
function Enter (obj,string) {
obj.expr.value+=string;
}
function Compute (obj) {//Click functions equal to the ' = ' button; Obj.expr.value=eval (Obj.expr.value);///Use the JS eval () method to compute the formula named expr in
</script>
<body>
<form name= "Calc" id= "Calc" action= "" >
<table border= "1" >
&LT;TD colspan= "4" ><input type= "text" name= "expr" size= "" action= "compute (this.form)"/></td>
<tr>
<td><input type= "button" value= "7" onclick= "Enter (This.form,seven)"/></td>
<td><input type= "button" value= "8" onclick= "Enter (this.form,eight)"/></td>
<td><input type= "button" value= "9" onclick= "Enter (this.form,nine)"/></td>
<td><input type= "button" value= "/" onclick= "Enter (this.form,divide)"/></td>
</tr>
<tr>
<td><input type= "button" value= "4" onclick= "Enter (this.form,four)"/></td>
<td><input type= "button" value= "5" onclick= "Enter (this.form,five)"/></td>
<td><input type= "button" value= "6" onclick= "Enter (this.form,six)"/></td>
<td><input type= "button" value= "*" onclick= "Enter (this.form,multiply)"/></td>
</tr>
<tr>
<td><input type= "button" value= "1" onclick= "Enter (this.form,one)"/></td>
<td><input type= "button" value= "2" onclick= "Enter (this.form,two)"/></td>
<td><input type= "button" value= "3" onclick= "Enter (this.form,three)"/></td>
<td><input type= "button" value= "-" onclick= "Enter (This.form,minus)"/></td>
</tr>
<tr>
&LT;TD colspan= "2" ><input type= "button" value= "0" onclick= "Enter (This.form,zero)"/></td>
<td><input type= "button" value= ". "Onclick=" Enter (this.form,decimal)/></td>
<td><input type= "button" value= "+" onclick= "Enter (this.form,plus)"/></td>
</tr>
<tr>
&LT;TD colspan= "2" ><input type= "button" value= "=" onclick= "Compute (this.form)"/></td> "
&LT;TD colspan= "2" ><input type= "button" value= "AC" onclick= "Form.reset ()"/></td>
</tr>
</table>
</form>
</body>

is not super NB code, the great God-Class!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.