js简易的计算器

代码如下(初学练习的小demo)

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>计算器</title>
	<style type="text/css">
		div{
			border: solid 1px;
			border-radius: 8px;
			width: 300px;
			height: 370px;
			margin: auto;
			margin-top: 70px;
			background-color: antiquewhite;
		}
		input[type=text]{
			width: 297px;
			height: 35px;
			margin-top: 25px;
			border: solid 1px;
			border-radius: 3px;
			background-color: aliceblue;
		}
		input[type=button]{
			width: 50px;
			height: 50px;
			margin: 10px;
			font-size: 30px;
			font-weight: bold;
		}
		input[type=button]:hover{
			color: aquamarine;
		}
	</style>
	<script type="text/javascript">
		function test(btn){
			var num=btn.value;
			switch(num){
				case "=":
				document.getElementById("int").value=eval(document.getElementById("int").value);
				break;
				case "c":
				document.getElementById("int").value="";
				break;
				default:
				document.getElementById("int").value=document.getElementById("int").value+num;
				break;
			}
		}
	</script>
</head>
<body>
	<div>
		<input type="text" name="int" id="int" readonly="readonly"/><br /><br />
		<input type="button" name="" id="" value="1" onclick="test(this)"/>
		<input type="button" name="" id="" value="2" onclick="test(this)"/>
		<input type="button" name="" id="" value="3" onclick="test(this)"/>
		<input type="button" name="" id="" value="c" onclick="test(this)"/>
		<input type="button" name="" id="" value="4" onclick="test(this)"/>
		<input type="button" name="" id="" value="5" onclick="test(this)"/>
		<input type="button" name="" id="" value="6" onclick="test(this)"/>
		<input type="button" name="" id="" value="+" onclick="test(this)"/>
		<input type="button" name="" id="" value="7" onclick="test(this)"/>
		<input type="button" name="" id="" value="8" onclick="test(this)"/>
		<input type="button" name="" id="" value="9" onclick="test(this)"/>
		<input type="button" name="" id="" value="-" onclick="test(this)"/>
		<input type="button" name="" id="" value="*" onclick="test(this)"/>
		<input type="button" name="" id="" value="0" onclick="test(this)"/>
		<input type="button" name="" id="" value="/" onclick="test(this)"/>
		<input type="button" name="" id="" value="=" onclick="test(this)"/>
	</div>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值