08-计算器案例

<html>
	<head>
		<title>计算器</title>
		<meta charset="UTF-8"/>
		<!--声明css代码域-->
		<style type="text/css">
		/*设置div样式*/
			#showdiv{
				border:  solid 1px;
				border-radius: 10px;/*设置边框角度*/
				width: 320px;
				height:400px;
				text-align: center;
				margin: auto;/*设置居中*/
				margin-top: 50px;
				background-color: floralwhite;	
			
				
			}
		/*设置输入框样式*/
			input[type=text]{
				margin-top: 20px;
				width: 290px;
				height: 40px;
				font-size: 20px;
				
			}
		/*设置按钮样式*/
			input[type=button]{
				width: 60px;
				height: 60px;
				margin-top: 20px;
				margin-left: 5px;
				margin-right: 5px;
				font-size: 30px;
				font-weight: bold;
				font-family: "萝莉体 第二版";
			}  
		</style>
		<!--声明js代码域-->
		<script type="text/javascript">
			//声明函数
			function test(btn){
				//获取button按钮对象的value值
				var num=btn.value;
				//根据用户点击动作执行对应的业务逻辑
				switch (num){
					case "=":
						document.getElementById("inp").value=eval(document.getElementById("inp").value);
						break;
					case "c":
						document.getElementById("inp").value="";
						break;
					default:
						//将按钮的值赋`值给input输入框
						document.getElementById("inp").value=document.getElementById("inp").value+num;
						break;
				}
			}
		</script>
	</head>
	<body>
		<div id="showdiv">
			<input type="text" name="" id="inp" value="" readonly="readonly"/><br />
			<input type="button" name="" id="btn" value="1"value="" οnclick="test(this)"/>
			<input type="button" name="" id="" value="2" οnclick="test(this)"/>
			<input type="button" name="" id="" value="3" οnclick="test(this)"/>
			<input type="button" name="" id="" value="4" οnclick="test(this)"/><br />
			<input type="button" name="" id="" value="5" οnclick="test(this)"/>
			<input type="button" name="" id="" value="6" οnclick="test(this)"/>
			<input type="button" name="" id="" value="7" οnclick="test(this)"/>
			<input type="button" name="" id="" value="8" οnclick="test(this)"/><br />
			<input type="button" name="" id="" value="9" οnclick="test(this)"/>
			<input type="button" name="" id="" value="+" οnclick="test(this)"/>
			<input type="button" name="" id="" value="-" οnclick="test(this)"/>
			<input type="button" name="" id="" value="*" οnclick="test(this)"/><br />
			<input type="button" name="" id="" value="0" οnclick="test(this)"/>
			<input type="button" name="" id="" value="/" οnclick="test(this)"/>
			<input type="button" name="" id="" value="c" οnclick="test(this)"/>
			<input type="button" name="" id="" value="=" οnclick="test(this)"/>
		</div>
	</body>
</html>

  

转载于:https://www.cnblogs.com/dream2060/p/10895675.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值