码迷,mamicode.com
首页 > Web开发 > 详细

js制作简单的计算器

时间:2016-07-24 16:06:19      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

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

<!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>

js制作简单的计算器

标签:

原文地址:http://www.cnblogs.com/MinLee/p/5700807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!