<html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
<head>
<title>计算器</title>
</head>
<script type="text/javascript" language="javascript">
function add(obj)
{
var temp=obj.value;//目的是在进行过一次运算后在下次点击之前保留数值,防治出现摁两次情形
document.getElementById("txt1").value+=obj.value;
var obf=document.getElementById("txt1").value;
if(obf.indexOf("=")>0)//当input里面已经有过一次运算的痕迹之后进行清空
{
clears();
document.getElementById("txt1").value=temp;//将缓存起来的值吐出来
}
}
function results()
{
var obj=document.getElementById("txt1").value;//在此仍是字符串
if(obj.indexOf("=")==-1)
document.getElementById("txt1").value+="="+eval(obj);//eval函数可以经字符串转换成运算结果
}
function clears()
{
document.getElementById("txt1").value="";
}
</script>
<body>
<table>
<tr>
<td><input type="button" οnclick="add(this)" id="Button1" value="1" /></td>
<td><input type="button" οnclick="add(this)" id="Button2" value="2" /></td>
<td><input type="button" οnclick="add(this)" id="Button3" value="3" /></td>
<td><input type="button" οnclick="add(this)" id="Button13" value="+" /></td>
</tr>
<tr>
<td><input type="button" οnclick="add(this)" id="Button4" value="4" /></td>
<td><input type="button" οnclick="add(this)" id="Button5" value="5" /></td>
<td><input type="button" οnclick="add(this)" id="Button6" value="6" /></td>
<td><input type="button" οnclick="add(this)" id="Button14" value="-" /></td>
</tr>
<tr>
<td><input type="button" οnclick="add(this)" id="Button7" value="7" /></td>
<td><input type="button" οnclick="add(this)" id="Button8" value="8" /></td>
<td><input type="button" οnclick="add(this)" id="Button9" value="9" /></td>
<td><input type="button" οnclick="add(this)" id="Button15" value="*" /></td>
</tr>
<tr>
<td><input type="button" οnclick="clears()" id="Button11" value="c" /></td>
<td><input type="button" οnclick="add(this)" id="Button10" value="0" /></td>
<td><input type="button" οnclick="results()" id="Button12" value="=" /></td>
<td><input type="button" οnclick="add(this)" id="Button16" value="/" /></td>
</tr>
</table>
<input type="text" id="txt1" />
</body>
</html>