如题所述
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>计算器</title>
</head>
<script language="javascript">
var op1,op2,opcode,result;
function getcodeadd()
{
opcode="+";
}
function getcodede()
{
opcode="-";
}
function getcodep1()
{
opcode="*";
}
function getcodedi()
{
opcode="/";
}
function count()
{
op1=parseFloat(document.getElementById("op1").value);
op2=parseFloat(document.getElementById("op2").value);
if(opcode=="+")
{document.getElementById("result").value=op1+op2;}
if(opcode=="-")
{document.getElementById("result").value=op1-op2;}
if(opcode=="*")
{document.getElementById("result").value=op1*op2;}
if(opcode=="/")
{document.getElementById("result").value=op1/op2;}
}
</script>
<body>
<table border="1" bgcolor="#3399FF">
<tr> <td height="60" colspan="3" ><b>计算器</b></td></tr>
<tr> <td width="140" height="38"> 第一个数</td>
<td width="216"> <input id="op1" type="text" size="14"/></td>
<td width="44" rowspan="3"><label>
<input type="submit" name="button1" id="button1" value="+" onclick="getcodeadd()"/>
<input type="submit" name="button2" id="button2" value="-" onclick="getcodede()"/>
<input type="submit" name="button3" id="button3" value="*" onclick="getcodep1()"/>
<input type="submit" name="button4" id="button4" value="/" onclick="getcodedi()"/>
</label></td>
</tr>
<tr> <td width="140" height="33"> 第二个数</td>
<td width="216"> <input id="op2" type="text" size="14"/></td>
</tr>
<tr> <td width="140" height="36"> 计算结果</td>
<td width="216"> <input id="result" type="text" size="14" onclick="count()"/></td>
</tr>
</table>
</body>
</html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>计算器</title>
</head>
<script language="javascript">
var op1,op2,opcode,result;
function getcodeadd()
{
opcode="+";
}
function getcodede()
{
opcode="-";
}
function getcodep1()
{
opcode="*";
}
function getcodedi()
{
opcode="/";
}
function count()
{
op1=parseFloat(document.getElementById("op1").value);
op2=parseFloat(document.getElementById("op2").value);
if(opcode=="+")
{document.getElementById("result").value=op1+op2;}
if(opcode=="-")
{document.getElementById("result").value=op1-op2;}
if(opcode=="*")
{document.getElementById("result").value=op1*op2;}
if(opcode=="/")
{document.getElementById("result").value=op1/op2;}
}
</script>
<body>
<table border="1" bgcolor="#3399FF">
<tr> <td height="60" colspan="3" ><b>计算器</b></td></tr>
<tr> <td width="140" height="38"> 第一个数</td>
<td width="216"> <input id="op1" type="text" size="14"/></td>
<td width="44" rowspan="3"><label>
<input type="submit" name="button1" id="button1" value="+" onclick="getcodeadd()"/>
<input type="submit" name="button2" id="button2" value="-" onclick="getcodede()"/>
<input type="submit" name="button3" id="button3" value="*" onclick="getcodep1()"/>
<input type="submit" name="button4" id="button4" value="/" onclick="getcodedi()"/>
</label></td>
</tr>
<tr> <td width="140" height="33"> 第二个数</td>
<td width="216"> <input id="op2" type="text" size="14"/></td>
</tr>
<tr> <td width="140" height="36"> 计算结果</td>
<td width="216"> <input id="result" type="text" size="14" onclick="count()"/></td>
</tr>
</table>
</body>
</html>
温馨提示:答案为网友推荐,仅供参考