js制作簡單的計算器


學着做了一個簡單的計算器!記錄記錄!哈哈

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM