案例事件委托实现计算器

<!DOCTYPE html PUBLIC "-//W3C//Dli XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/Dli/xhtml1-transitional.dli">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>简易计算器</title>
<style type="text/css"  />

* { padding: 0; margin: 0; }
li { list-style: none; }
body { background: #940032; }

#counter { width: 500px; height: 420px; background:#939; margin: 50px auto 0; position: relative; }
#counter h2 { line-height: 42px; padding-left: 15px; font-size: 14px; font-family: arial; color: #ff3333; }
#counter a { font-weight: normal; text-decoration: none; color: #ff3333; }
#counter a:hover { text-decoration: underline; }
#bg { width: 280px; height: 200px; border: 3px solid #680023; background: #990033; filter: alpha(opacity=80); opacity: 0.8; position: absolute; left: 50%; top: 115px; margin-left: -141px; }
#counter_content { width: 250px; position: absolute; top: 130px; left: 130px; z-index: 1; }
#counter_content h3 { margin-bottom: 10px; }
#counter_content h3 input { border: none; width: 223px; height: 30px; line-height: 30px; padding: 0 10px; background: url(images/ico.png) no-repeat; text-align: right; color: #333; font-size: 14px; font-weight: bold; }
#counter_content div { width: 250px; }
#counter_content input { width: 60px; height: 30px; line-height: 30px; float: left; background: url(images/ico.png) no-repeat -303px 0; text-align: center; color: #fff; cursor: pointer; margin: 0 1px 4px 0; border:0; }
#counter_content div > input:hover { background: url(images/ico.png) no-repeat -243px 0; }
#counter p { width: 500px; position: absolute; bottom: 20px; left: 0; color: #ff3333; text-align: center; font-size: 12px; }
</style>
</head>
</head>

<body>

<div id="counter">
	<h2>简易计算</h2>
	<div id="counter_content">
		<h3 style="display: block;"><input id="input2" type="text" value="" /></h3>
		<h3><input id="input1" type="text" value="" /></h3>
		<div id="div1">
            <input type="button" value="7"  />
            <input type="button" value="8" />
            <input type="button" value="9"  />
            <input type="button" value="+"  />
            <input type="button" value="4"  />
            <input type="button" value="5"  />
            <input type="button" value="6"  />
            <input type="button" value="-"  />
            <input type="button" value="1"  />
            <input type="button" value="2"  />
            <input type="button" value="3"  />
            <input type="button" value="*"  />
            <input type="button" value="0"  />
            <input type="button" value="C"  />
            <input type="button" value="="  />
            <input type="button" value="/"  />
		</div>
	</div>
</div>
</body>
</html>
<script src="public.js"></script>
<script type="text/javascript">
var fuhao="";
var flag=false;
$id("div1").onclick=function(){
	var e=e||event;
	var target=e.target||e.srcElement;
	if(target.nodeName.toLowerCase()=="input"){
		var val=target.value;
		switch(val){
			case"+":
			case"-":
			case"*":
			case"/":
			fuhao=val;
			if($id("input2").value==""){
				$id("input2").value=$id("input1").value;
			}else{
				$id("input1").value=jiShuan(fuhao);
			}
			flag=true;
			$id("input2").value=$id("input1").value;
			break;
			case"=":
			$id("input1").value=jiShuan(fuhao);
			break;
			case"C":
			$id("input2").value = "";
            $id("input1").value = "";
            fuhao="";
            flag=false;
			break;
			default:
			if(flag){
				$id("input1").value="";
				flag=false;
			}
			$id("input1").value+=val;
		}
	}
}
function jiShuan(fuhao){
	switch(fuhao){
		case"+":return Number($id("input2").value)+Number($id("input1").value);
		case"-":return Number($id("input2").value)-Number($id("input1").value);
		case"*":return Number($id("input2").value)*Number($id("input1").value);
		case"/":return Number($id("input2").value)/Number($id("input1").value);
		default:return Number($id("input1").value);
	}
}
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值