使用JavaScript 编写简单计算器
本文方法超级简单,思路非常的值得推荐,小伙伴们参考下吧
<!DOCTYPEhtml>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>javascript简单计算器</title>
<script>
//定义全局变量 varone="1";
vartwo="2";
varthree="3";
varfour="4";
varfive="5";
varsix="6";
varseven="7";
vareight="8";
varnine="9";
varzero="0";
varplus="+",minus="-",multiply="*",divide="/",decimal=".";
functionenter(obj,string){
obj.expr.value+=string;
}
functioncompute(obj){//单击等于"="按钮调用的函数; obj.expr.value=eval(obj.expr.value);//用JS得eval()方法计算name为expr中的公式 }
</script>
</head>
<body>
<formname="calc"id="calc"action="">
<tableborder="1">
<tdcolspan="4"><inputtype="text"name="expr"size="30"action="compute(this.form)"/></td>
<tr>
<td><inputtype="button"value="7"onclick="enter(this.form,seven)"/></td>
<td><inputtype="button"value="8"onclick="enter(this.form,eight)"/></td>
<td><inputtype="button"value="9"onclick="enter(this.form,nine)"/></td>
<td><inputtype="button"value="/"onclick="enter(this.form,divide)"/></td>
</tr>
<tr>
<td><inputtype="button"value="4"onclick="enter(this.form,four)"/></td>
<td><inputtype="button"value="5"onclick="enter(this.form,five)"/></td>
<td><inputtype="button"value="6"onclick="enter(this.form,six)"/></td>
<td><inputtype="button"value="*"onclick="enter(this.form,multiply)"/></td>
</tr>
<tr>
<td><inputtype="button"value="1"onclick="enter(this.form,one)"/></td>
<td><inputtype="button"value="2"onclick="enter(this.form,two)"/></td>
<td><inputtype="button"value="3"onclick="enter(this.form,three)"/></td>
<td><inputtype="button"value="-"onclick="enter(this.form,minus)"/></td>
</tr>
<tr>
<tdcolspan="2"><inputtype="button"value="0"onclick="enter(this.form,zero)"/></td>
<td><inputtype="button"value="."onclick="enter(this.form,decimal)"/></td>
<td><inputtype="button"value="+"onclick="enter(this.form,plus)"/></td>
</tr>
<tr>
<tdcolspan="2"><inputtype="button"value="="onclick="compute(this.form)"/></td>
<tdcolspan="2"><inputtype="button"value="AC"onclick="form.reset()"/></td>
</tr>
</table>
</form>
</body>
</html>
是不是超级NB的代码,大神级的!