计算器简单实现
<html>
<head>
<script>
var flag = true;
function getNum(num){
if(!flag){
document.getElementById('res').value = '';
flag = true;
}
document.getElementById('res').value += num;
}
function getResult(){
var result = eval(document.getElementById('res').value);
document.getElementById('res').value = result;
flag = false;
}
</script>
</head>
<body>
<table border='1px' width='300px'>
<caption>计算器</caption>
<tr>
<td colspan='4'><input type='text' id='res' size=47></td>
</tr>
<tr>
<td><input type='button' value='1' style="width:75px" onclick="getNum(1)"></td>
<td><input type='button' value='2' style="width:75px" onclick="getNum(2)"></td>
<td><input type='button' value='3' style="width:75px" onclick="getNum(3)"></td>
<td><input type='button' value='+' style="width:75px" onclick="getNum('+')"></td>
</tr>
<tr>
<td><input type='button' value='4' style="width:75px" onclick="getNum(4)"></td>
<td><input type='button' value='5' style="width:75px" onclick="getNum(5)"></td>
<td><input type='button' value='6' style="width:75px" onclick="getNum(6)"></td>
<td><input type='button' value='-' style="width:75px" onclick="getNum('-')"></td>
</tr>
<tr>
<td><input type='button' value='7' style="width:75px" onclick="getNum(7)"></td>
<td><input type='button' value='8' style="width:75px" onclick="getNum(8)"></td>
<td><input type='button' value='9' style="width:75px" onclick="getNum(9)"></td>
<td><input type='button' value='*' style="width:75px" onclick="getNum('*')"></td>
</tr>
<tr>
<td><input type='button' value='0' style="width:75px" onclick="getNum(0)"></td>
<td><input type='button' value='.' style="width:75px" onclick="getNum('.')"></td>
<td><input type='button' value='=' style="width:75px" onclick="getResult()"></td>
<td><input type='button' value='/' style="width:75px" onclick="getNum('/')"></td>
</tr>
</table>
</body>
</html>