JavaScript计算器(加减乘除完善)

4 篇文章 0 订阅
2 篇文章 0 订阅

在这里插入图片描述

<head>
		<meta charset="UTF-8">
		<title></title>
		<!--css样式-->
		<style type="text/css">
			table,tr,td{
				width: 300px;
				height: 30px;
				border: 1px solid blue;
				border-collapse: collapse;
				text-align: center;
			}
			body{
				width: 300px;
				margin: 20px auto;
			}
			td{
				background: #7FFF00;
			}
			td:hover{
				background: #993399;
			}
		</style>
		<!--js代码-->
		<script type="text/javascript">
			//通过创建元素将表格写出来
			function drawTab(ary){
				var table = document.getElementById("tab");
				for(var i = 0;i<ary.length;i++){
					//调用init函数里面的ary数组并对数组进行遍历
					var temp = ary[i]
					var tr = document.createElement("tr");
					table.appendChild(tr);
					for(var j = 0;j<temp.length;j++){
						var td = document.createElement("td")
						td.innerText = temp[j];
						//给每一个td添加一个点击事件
						td.setAttribute("onclick","calc(this)");
						tr.appendChild(td);
					}
				}
			}
			//设置一个真值
			var flag = true;
			function calc(num){
				var result = document.getElementById("result");
				//使用switch语句进行判断
				switch(num.innerText){
					//判断如果为等号的时候使用eval()函数进行计算,并将真值赋值为false
					case "=":
						if(result.innerText==""){
							return;
						}
						//通过replace替换字符串
						result.innerText = result.innerText.replace("×","*");
						result.innerText = result.innerText.replace("÷","/");
						result.innerText = eval(result.innerText);
						flag = false;
						break;
					//通过使用substr对字符串进行截取,删除最后一个字符
					case "←":
						result.innerText = result.innerText.substr(0,result.innerText.length-1);
						break;
					//如果点击+-×÷%时不再进行叠加,将前面的给替换掉
					case "+":
					case "-":
					case "×":
					case "÷":
					case "%":
					case ".":
						if(result.innerText==""){
							return;
						}
						if(result.innerText.substr(result.innerText.length-1,result.innerText.length)=="+"||
						result.innerText.substr(result.innerText.length-1,result.innerText.length)=="-"||
						result.innerText.substr(result.innerText.length-1,result.innerText.length)=="×"||
						result.innerText.substr(result.innerText.length-1,result.innerText.length)=="÷"||
						result.innerText.substr(result.innerText.length-1,result.innerText.length)=="%"||
						result.innerText.substr(result.innerText.length-1,result.innerText.length)=="."){
							result.innerText = result.innerText.substr(0,result.innerText.length-1);
						}
						result.innerText += num.innerText;
						flag = true;
						break;
					//清除操作
					case "c":
						result.innerText = "";
						break;
					//点击0-9是进行输入,并在点击等号之后再点击0-9时将输出清空,进行下一次计算
					default:
						if(flag){
							
						}else{
							result.innerText = "";
							flag = true;
						}
						result.innerText += num.innerText;
						break;
				}
			}
			//函数调用
			function init(){
				//计算器上面要显示的数字
				var ary = [
				["%", "c", "←", "÷"],
				["7", "8", "9", "×"],
				["4", "5", "6", "-"],
				["1", "2", "3", "+"],
				["", "0", ".", "="]
			];
				drawTab(ary)
			}
		</script>
	</head>
	<body onload="init()">
		<!--table表格-->
		<table id="tab">
			<tr>
				<td colspan="4">简易计算器</td>
			</tr>
			<tr>
				<td colspan="4" id="result"></td>
			</tr>
		</table>
	</body>
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值