zoukankan      html  css  js  c++  java
  • 07 JS+HTML 完成一个简单的计算器

    先看效果:

    关键代码:

    js中:

    document.getElementById().value;获取标签的值。可以用它来获取输入框的值,以及赋值。同样滴可以用它获取按钮的值。

    不多说了,上代码

     html:

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title>计算器</title>
    		<link rel="stylesheet" type="text/css" href="css/multiple.css"/>
    	</head>
    	<body>
    		<script type="text/javascript">
    			var flag = true;//document.getElementById("screen").value为空时
    			var lastChar =-1; //上一个按钮的值
    			function test(btn){
    				if(document.getElementById("screen").value=="520——我也爱你"||document.getElementById("screen").value=="受不了了!!!"){
    					document.getElementById("screen").value="";
    					flag = true;
    				}
    				var v = btn.value;//获取按钮的值
    				if(flag&&(v=="*"||v=="/"||v=="="||v=="+")){
    					document.getElementById("screen").value="";
    				}else if("C"==v){
    					document.getElementById("screen").value="";
    					flag = true;
    				}else if("="==v){
    					doMath();
    				}else{
    					if(!(("+"==lastChar||"-"==lastChar||"*"==lastChar||"/"==lastChar)&&(v=="+"||v=="-"||v=="*"||v=="/"||v=="="))){
    						if(document.getElementById("screen").value.length<17){//太长了就不让输入了
    						document.getElementById("screen").value+=v;
    						lastChar = v;
    						flag = false;
    						}
    					}
    				}
    			}
    			function doMath(){//对拼接的字符串进行运算
    				document.getElementById("screen").value = eval(document.getElementById("screen").value);
    				if(document.getElementById("screen").value.length>18){
    					document.getElementById("screen").value="受不了了!!!";
    				}
    				if(document.getElementById("screen").value=="520"){
    					document.getElementById("screen").value="520——我也爱你"
    				}
    			}
    		</script>
    		<div id="frame">
    			<div id="screen-warp">
    				<input type="text" id="screen" value="" />
    			</div>
    			<div id="button-wrap">
    				<div id="number-wrap">
    					<table id="number">
    						<tr>
    							<td><button type="button" onclick="test(this)" value="1">1</button></td>
    							<td><button type="button" onclick="test(this)" value="2">2</button></td>
    							<td><button type="button" onclick="test(this)" value="3">3</button></td>
    						</tr>
    						<tr>
    							<td><button type="button" value="4"  onclick="test(this)">4</button></td>
    							<td><button type="button" value="5"  onclick="test(this)">5</button></td>
    							<td><button type="button" value="6"  onclick="test(this)">6</button></td>
    						</tr>
    						<tr>
    							<td><button type="button" value="7"  onclick="test(this)">7</button></td>
    							<td><button type="button" value="8"  onclick="test(this)">8</button></td>
    							<td><button type="button" value="9"  onclick="test(this)">9</button></td>
    						</tr>
    						<tr>
    							<td><button type="button" value="C"  onclick="test(this)">C</button></td>
    							<td><button type="button" value="0"  onclick="test(this)">0</button></td>
    							<td><button type="button" value="="  onclick="test(this)">=</button></td>
    						</tr>
    					</table>
    				</div>
    				<div id="character">
    					<div><button type="button" value="+" onclick="test(this)">+</button></div>
    					<div><button type="button" value="-" onclick="test(this)">-</button></div>
    					<div><button type="button" value="*" onclick="test(this)">*</button></div>
    					<div><button type="button" value="/" onclick="test(this)">÷</button></div>
    				</div>
    			</div>
    		</div>
    	</body>
    </html>
    

      

    css文件

    html{
    	padding: 0;
    	margin: 0;
    	background-color: #7494c8;
    }
    #frame{
    	border:1px solid black;
    	380px;
    	height:500px;
    	margin: auto;
    	margin-top: 10px;
    	background-color: #9b95a7;
    }
    #screen-warp{
    	 95%;
    	height: 100px;
    	margin:10px;
    }
    #screen{
    	 98%;
    	height: 90%;
    	border:1px black solid;
    	font-size: 33px;
    }
    #number-wrap{
    	 68%;
    	height: 360px;
    	margin-left:10px;
    	float: left;
    }
    #number{
    	100%;
    	height: 100%;
    }
    #number tr td{
    	height: 25%;
    	 33%;
    	border:  solid 1px;
    	text-align: center;
    }
    
    #character{
    	height: 359px;
    	 24%;
    	float: left;
    	margin-left:10px;
    }
    #character div{
    	height: 25%;
    	text-align: center;
    	line-height: 84px;
    	border: 1px solid;
    }
    #character div{
    	margin:auto;
    	margin-top: 2px;
    	height: 85px;
    	 90%;
    }
    button{
    	 100%;
    	height: 100%;
    	font-size: 30px;
    }
    

      

  • 相关阅读:
    解决The current branch is not configured for pull No value for key branch.master.merge found in confi
    使用Eclipse构建Maven项目 (step-by-step)
    ecpilise引入Maven项目目录不正常,无JRE,无Maven Dependencies
    解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法
    Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:RELEASE from any of the configured repositories.
    (转) launch failed.Binary not found in Linux/Ubuntu解决方案
    .c_str()/atoi()/
    (转)Should I use char** argv or char* argv[]
    (转) int argc, char* argv[] 的用法
    (转) 制作 Clonezilla live 启动盘
  • 原文地址:https://www.cnblogs.com/Scorpicat/p/12204983.html
Copyright © 2011-2022 走看看