效果图如下:
下面是源代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的计算器</title>
<center>计算器</center>
<br/>
<style>
#circle{
width:50px; height:50px;
font-size: 26px;
border-radius:50%;
color:white;
background: dimgray;
}
</style>
<script language="JavaScript">
function demo(val)
{
document.getElementById("txt").value+=val; //把输入的数字传输到文本框
}
function showResolt()
{
var a=document.getElementById("txt").value;
var vb=eval(a);
document.getElementById("txt").value=vb;
}
</script>
</head>
<body>
<table
align="center"
border="1"
cellspacing="0">
<tr >
<td colspan="4">
<input type="text" value="" readonly="readonly"
style="width: 215px; height: 50px; font-size: 26px; background: cornsilk" id ="txt" />
</td>
</tr>
<tr>
<td><input type="button" value="7"id="circle" onclick="demo(this.value)"/> </td>
<td><input type="button" value="8"id="circle" onclick="demo(this.value)"/> </td>
<td><input type="button" value="9"id="circle" onclick="demo(this.value)"/> </td>
<td><input type="button" value="+"id="circle" onclick="demo(this.value)"/> </td>
</tr>
<tr>
<td><input type="button" value="4"id="circle"onclick="demo(this.value)"/> </td>
<td><input type="button" value="5"id="circle"onclick="demo(this.value)"/> </td>
<td><input type="button" value="6"id="circle"onclick="demo(this.value)"/> </td>
<td><input type="button" value="-"id="circle"onclick="demo(this.value)"/> </td>
</tr>
<tr>
<td><input type="button" value="1"id="circle"onclick="demo(this.value)"/> </td>
<td><input type="button" value="2"id="circle"onclick="demo(this.value)"/> </td>
<td><input type="button" value="3"id="circle"onclick="demo(this.value)"/> </td>
<td><input type="button" value="*"id="circle"onclick="demo(this.value)"/> </td>
</tr>
<tr>
<td><input type="button" value="0"id="circle"onclick="demo(this.value)"/> </td>
<td><input type="button" value="."id="circle"onclick="demo(this.value)"/> </td>
<td><input type="button" value="/"id="circle"onclick="demo(this.value)"/> </td>
<td><input type="button" value="="id="circle"onclick="showResolt(this.value)"/> </td>
</tr>
</table>
</body>
</html>