HTML5+js实现一个简单的计算器
全部代码:
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>简单的计算器</title>
	</head>
	<body onload="onload()">
		<div style="width: 12.25rem;height: 9.5rem;background-color: #00B3EE;">
			<div id="shuru">
				<div style="margin-top: 0.3125rem;width: 9.375rem;height: 2.5rem;text-align: right;padding-right: 0.625rem;font-size: 1.25rem;">
					<input type="text" id="screenName" onfocus="jsq(this)" style="text-align: right;"/>
				</div>
			</div>
			<div>
				<input type="button" id="7" onclick="jsq(this.id)" value="7" style="width: 1.875rem;" />
				<input type="button" id="8" onclick="jsq(this.id)" value="8" style="width: 1.875rem;" />
				<input type="button" id="9" onclick="jsq(this.id)" value="9" style="width: 1.875rem;" />
				<input type="button" id="back" onclick="huitui()" value="回退" style="font-size: 0.625rem;" />
				<input type="button" id="c" onclick="qingchu()" value="清除" style="font-size: 0.625rem;" />
				<br />
				<input type="button" id="4" onclick="jsq(this.id)" value="4" style="width: 1.875rem;" />
				<input type="button" id="5" onclick="jsq(this.id)" value="5" style="width: 1.875rem;" />
				<input type="button" id="6" onclick="jsq(this.id)" value="6" style="width: 1.875rem;" />
				<input type="button" id="*" onclick="jsq(this.id)" value="*" style="width: 1.875rem;" />
				<input type="button" id="/" onclick="jsq(this.id)" value="/" style="width: 1.875rem;" />
				<br />
				<input type="button" id="1" onclick="jsq(this.id)" value="1" style="width: 1.875rem;" />
				<input type="button" id="2" onclick="jsq(this.id)" value="2" style="width: 1.875rem;" />
				<input type="button" id="3" onclick="jsq(this.id)" value="3" style="width: 1.875rem;" />
				<input type="button" id="+" onclick="jsq(this.id)" value="+" style="width: 1.875rem;" />
				<input type="button" id="-" onclick="jsq(this.id)" value="-" style="width: 1.875rem;" />
				<br />
				<input type="button" id="0" onclick="jsq(this.id)" value="0" style="width: 1.875rem;" />
				<input type="button" id="00" onclick="jsq(this.id)" value="00" />
				<input type="button" id="." onclick="jsq(this.id)" value="." style="width: 1.875rem;" />
				<input type="button" id="%" onclick="jsq(this.id)" value="%" style="width: 1.875rem;" />
				<input type="button" id="deng" onclick="deng()" value="=" style="width: 1.875rem;" />
			</div>
		</div>
		<script>
			var num = 0;
			function jsq(num){
				document.getElementById('screenName').value += document.getElementById(num).value;
			}
			function deng(){
				document.getElementById('screenName').value = eval(document.getElementById("screenName").value);
			}
			function qingchu(){
				document.getElementById("screenName").value = null;
				document.getElementById("screenName").focus();
			}
			function huitui(){
				var arr = document.getElementById("screenName");
				arr.value = arr.value.substring(0,arr.value.length - 1);
			}
			function onload(){
				document.getElementById("screenName").focus();
				
			}
		</script>
	</body>
</html>
                    
                
                
            
        
浙公网安备 33010602011771号