js 计算器案例

<html>
	<head>
		<title>js 加法器</title>
		<meta charset="utf-8"/>
		<!--js-->
		<script type="text/javascript">
			function test(btn){  //因为js只有一种类型var,所以形类型可以省略
				/*获取button按钮对象及其属性的值*/
				var num=btn.value;  //btn为对象
				/*将按钮值赋值给input输入框*/
				switch(num)
				{
					case "=":
							/*eval()函数将字符串转换成可执行的js代码,即执行算数运算*/
						document.getElementById("inp").value=eval(document.getElementById("inp").value);
						break;
					case "c":
						document.getElementById("inp").value="";
						break;
					default:
					document.getElementById("inp").value+=num;
					break;
				}
				
			}
			
			
		</script>
		<!--css-->
		<style type="text/css">
			#showdiv{
				border:solid 1px;
				border-radius: 5px;
				width:310px;
				height:400px;
				text-align: center;
				margin:auto;
				margin-top:50px;
				background-color: antiquewhite;
			}
			input[type=text]{
				margin-top:10px;
				width:290px;
				height:40px;
				font-size:25px;
			}
			input[type=button]{
				width:60px;
				height:60px;
				margin-top:20px;
				margin-left:5px;
				margin-right:5px;
				font-size:30px;
				font-weight: bold;
			}
		</style>
	</head>
	<body>
		<h3 align="center">js 加法器</h3>
		<hr size="30" color="aquamarine"/>
		
		<div id="showdiv">                            <!--readonly只能点不能键盘写-->
			<input type="text" name="" id="inp" value="" readonly="readonly" /><br />
			<input type="button" name="" id="btn" 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="4"onclick="test(this)"/><br />
			<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="7"onclick="test(this)"/>
			<input type="button" name="" id="" value="8"onclick="test(this)"/><br />
			<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="*"onclick="test(this)"/><br />
			<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="c"onclick="test(this)"/>
			<input type="button" name="" id="" value="="onclick="test(this)"/>
			
			
			
		</div>
	</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值