javascript制作简易计算器

来源:互联网 发布:python split函数 编辑:程序博客网 时间:2024/04/28 05:21


<html>
 <head>
 <script type="text/javascript">
 var kg=true;
function getval(val){
if(!kg){
document.getElementById('res').value='';
kg=true;
}
document.getElementById('res').value+=val;
}
function getres(val){
var as=document.getElementById('res').value;
as=eval(as);
document.getElementById('res').value=as;
kg=false;
}
 </script>
 </head>


 <body>
  <table border='1px' width='220px'>
<tr colspan='4'>
    <input type='text' id='res' size='42'>
</tr>
<tr>
<td><input type='button' value='1' style="width:70px;" onclick="getval(1)"></td>
<td><input type='button' value='2' style="width:70px;" onclick="getval(2)"/></td>
<td><input type='button' value='3' style="width:70px;" onclick="getval(3)"/></td>
<td><input type='button' value='+' style="width:70px;" onclick="getval('+')"/></td>
</tr>
<tr>
<td><input type='button' value='4' style="width:70px;" onclick="getval(4)"></td>
<td><input type='button' value='5' style="width:70px;" onclick="getval(5)"/></td>
<td><input type='button' value='6' style="width:70px;" onclick="getval(6)"/></td>
<td><input type='button' value='-' style="width:70px;" onclick="getval('-')"/></td>
</tr>
<tr>
<td><input type='button' value='7' style="width:70px;" onclick="getval(7)"></td>
<td><input type='button' value='8' style="width:70px;" onclick="getval(8)"/></td>
<td><input type='button' value='9' style="width:70px;" onclick="getval(9)"/></td>
<td><input type='button' value='*' style="width:70px;" onclick="getval('*')"/></td>
</tr>
<tr>
<td><input type='button' value='0' style="width:70px;" onclick="getval(0)"></td>
<td><input type='button' value='.' style="width:70px;" onclick="getval(.)"/></td>
<td><input type='button' value='=' style="width:70px;" onclick="getres('=')"/></td>
<td><input type='button' value='/' style="width:70px;" onclick="getval('/')"/></td>
</tr>
  </table>
 </body>
</html>