简易计算器用js实现的代码
<html>
<meta charset="utf-8"/>
<head>
<script type='text/javascript'>
var flag=true;
function getNum(num){
if(!flag){
document.getElementById('res').value ='';
flag=true;
}
document.getElementById('res').value +=num;
}
function getRes(num){
var num=document.getElementById('res').value;
num=eval(num);
document.getElementById('res').value=num;
flag=false;
}
</script>
</head>
<body>
简易计算器
<TABLE border="1px" width="240px">
<TR>
<TD colspan="4" ><input type="text" style="width:340px;" id='res'/></TD>
</TR>
<TR>
<TD><input type="button" value="1" onclick='getNum(1)' style="width:80px;"/></TD>
<TD><input type="button" value="2" onclick='getNum(2)' style="width:80px;"></TD>
<TD><input type='button' value='3' onclick='getNum(3)' style="width:80px;"/></TD>
<TD><input type='button' value='+' onclick="getNum('+')" style="width:80px;"/></TD>
</TR>
<TR>
<TD><input type='button' value='4' onclick="getNum(4) " style="width:80px;"/></TD>
<TD><input type='button' value='5' onclick="getNum(5)" style="width:80px;"/></TD>
<TD><input type='button' value='6' onclick="getNum(6)" style="width:80px;"/></TD>
<TD><input type='button' value='-' onclick="getNum('-')" style="width:80px;"/></TD>
</TR>
<TR>
<TD><input type='button' value='7' onclick="getNum(7)" style="width:80px;"/></TD>
<TD><input type='button' value='8' onclick="getNum(8)" style="width:80px;"/></TD>
<TD><input type='button' value='9' onclick="getNum(9)" style="width:80px;"/></TD>
<TD><input type='button' value='*' onclick="getNum('*')" style="width:80px;"/></TD>
</TR>
<TR>
<TD><input type='button' value='0' onclick="getNum(0)" style="width:80px;"/></TD>
<TD><input type='button' value='.' onclick="getNum('.')" style="width:80px;" /></TD>
<TD><input type='button' value='=' onclick="getRes('=')" style="width:80px;"/></TD>
<TD><input type='button' value='/' onclick="getNum('/')" style="width:80px;"/></TD>
</TR>
</TABLE>
</body>
</html>