计算器 案例

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
	  .panel {
        	border: 4px solid #ccc;
        	width: 202px;
        	margin: 50px auto;
  		}
  		.panel p,.panel input{
  			font-family: "黑体";
  			font-size:20px;
  			margin:5px;
  			float:left;
  		}
  		.panel p{
  			border:4px solid #ddd;
  			height:30px;
  			width:123px;
  			padding:5px;
  		}
  		.panel input{
  			height:40px;
  			width:40px;
  		}
</style>
 <script type="text/javascript">
      function cal(e) {
        var obj = e.target || e.srcElement;
        if(obj.nodeName != "INPUT") {
          return;
        }
        
        var value = obj.value;
        var screen = document.getElementById("screen");
        if(value=="C") {
          screen.innerHTML = "";
        } else if (value=="=") {
          var exp = screen.innerHTML;
          try {
            var r = eval("("+exp+")");
            screen.innerHTML = r;
          } catch(e) {
            console.log(e);
            screen.innerHTML = "Error";
          }
        } else {
          screen.innerHTML += value;
        }
      }
    </script>
</head>
<body>
	  <div class="panel" οnclick="cal(event);">
		
		<div>
			
			 <p id="screen"></p>
       		 <input type="button" value="C">
        	  <div style="clear:both"></div>

		</div>
		
		<div>
        <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=".">
        <input type="button" value="=">
        <input type="button" value="+">
        
        <div style="clear:both"></div>
       
      </div>
		
	</div>
	
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值