js制作简单的计算器

时间:2021-11-25 09:07:22

学着做了一个简单的计算器!记录记录!哈哈

<!DOCTYPE html>

<html>

<head>

<title>简单的计算器</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 <style type="text/css">

       input{width:100%}

    </style>

</head>

<body>

     <script type="text/javascript">

      function compute(obj)

      {

       obj.expr.value=eval(obj.expr.value)

      }

     var plus="+";

var minus="-";

var multiply="*";

var divide="/";

var decimal=".";

function enter(obj,string)

{

obj.expr.value+=string;

}

function cle(obj)

{

obj.expr.value='';

}

</script>

<form >

<table border=1>

<tr >

<td colspan="4"><input type="text" name="expr" size="30"  style="width:97%"></td>

</tr>

<tr>

<td><input type="button" value="7" onclick="enter(this.form,7)"></td>

<td><input type="button" value="8" onclick="enter(this.form,8)"></td>

<td><input type="button" value="9" onclick="enter(this.form,9)"></td>

<td><input type="button" value="/" onclick="enter(this.form,divide)"></td>

</tr>

<tr>

<td><input type="button" value="4" onclick="enter(this.form,4)"></td>

<td><input type="button" value="5" onclick="enter(this.form,5)"></td>

<td><input type="button" value="6" onclick="enter(this.form,6)"></td>

<td><input type="button" value="*" onclick="enter(this.form,multiply)"</td>

</tr>

<tr>

<td><input type="button" value="1" onclick="enter(this.form,1)"></td>

<td><input type="button" value="2" onclick="enter(this.form,2)"></td>

<td><input type="button" value="3" onclick="enter(this.form,3)"></td>

<td><input type="button" value="-" onclick="enter(this.form,minus)"></td>

</tr>

<tr>

<td colspan="2"><input type="button" value="0" onclick="enter(this.form,0)"></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>

<td colspan="2" ><input type="button" value="=" onclick="compute(this.form)" ></td>

<td colspan="2"><input type="button" value="AC" size=3 onclick="cle(this.form)"></td>

</tr>

</table>

</form>

</body>

</html>