网页版计算器

博客内容提及直接上传代码,但未给出更多关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上传代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>网页版计算器</title>
		<style>
			input{
				width: 397px;
				height: 50px;
				font-size: 30px;
			}
			tr{
				text-align: center;
				
			}
			td{
				width: 100px;
				height: 119px;
				font-size: 30px;
			}
			td:hover{
				background-color: greenyellow;
				cursor: pointer;
				
			}
		</style>
	</head>
	<body>
		<center><input type="text" value="0" disabled="disabled" id="show"/></center>
		<table border="1px" cellspacing="0" cellpadding="0" width="400px" height="600px" align="center">
			<tr>
				<td id="clear">C</td>
				<td id="del">退格</td>
				<td class="operator">%</td>
				<td class="operator">/</td>
			</tr>
			<tr>
				<td class="num">7</td>
				<td class="num">8</td>
				<td class="num">9</td>
				<td class="operator">*</td>
			</tr>
			<tr>
				<td class="num">4</td>
				<td class="num">5</td>
				<td class="num">6</td>
				<td class="operator">-</td>
			</tr>
			<tr>
				<td class="num">1</td>
				<td class="num">2</td>
				<td class="num">3</td>
				<td class="operator">+</td>
			</tr>
			<tr>
				<td colspan="2" class="num">0</td>
				<td class="num">.</td>
				<td id="result">=</td>
			</tr>
		</table>
	</body>
	<script>
	  	var showresult = document.getElementById("show");
		var clear = document.getElementById("clear");
		var del = document.getElementById("del");
		var result = document.getElementById("result");
		var nums = document.getElementsByClassName("num");
		var numvalue1 = "";
		var numvalue2 = "";
		var oper = "";
		for(var i=0; i<nums.length; i++){
			nums[i].onclick = function(){
				numvalue1 += this.innerText;
				showresult.value = numvalue1;
			}
		}
		var operators = document.getElementsByClassName("operator");
		for(var i=0; i < operators.length; i++){
			operators[i].onclick = function(){
				if(numvalue2 == ""){
					numvalue2 = numvalue1;
					numvalue1 = "";
					oper = this.innerText;
				} else{
					if(numvalue1 != ""){
						resultFn();
					}
					oper = this.innerText;
				}
			}
		}
		
		clear.onclick = function(){
			numvalue1 = "";
			numvalue2 = "";
			oper = "";
			showresult.value = 0;
		}
		
		del.onclick = function(){
			if(numvalue1.length >=1){
				numvalue1 = numvalue1.substring(0,numvalue1.length-1);
				showresult.value = numvalue1;
			}
		}
		
		result.onclick = function(){
			resultFn();
		}
		function resultFn(){
			var one = new Number(numvalue2);
			var two = new Number(numvalue1);
			var r = null;
			switch(oper){
				case "+":
					r = one + two;
					break;
				case "-":
					r = one - two;
					break;
				case "*":
					r = one * two;
					break;
				case "/":
					if(two == 0){
						r = 0;
					} else{
						r = one / two;
					}
					break;
				case "%":
					if(two == 0){
						r = 0;
					} else {
						r = one % two;	
					}
					break;
				default:
					alert("输入有误");
			}
			numvalue2 = new Number(r).toFixed(6);
			numvalue1 = "";
			showresult.value = numvalue2*1;
		}
	</script>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值