原生JavaScript写的简单的计算器

我就不废话了,预览地址http://jsq.nxn.ink/
代码在下面。

<!DOCTYPE html>
<html>
 <head> 
 <meta content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <title>简单的计算器</title> 
  
  <style>
 	input[type=button]{
			border-radius: 5px;
			width: 55px;
			height: 30px;
			margin: 2px;
			font-size: 20px;
			}
  </style>
 </head> 
 <body> 
<script>
//把数字的字赋给输入框的value,
 function calCulate(val){
var t=document.getElementById("t").value;//先得到当前输入框的value值,
document.getElementById("t").value=t+val;//然后拼接字符串,把以前的制和现在的拼接起来
document.getElementById("sDiv").innerHTML=t+val;//把上一步计算写入页面,显示在输入框的上面
}
//把空字符串赋给输入框的值value,相当于清空输入框的value,上一步计算清空也是这样
function c(){
document.getElementById("t").value="";
document.getElementById("sDiv").innerHTML="";
}
//结果计算等于,用的eval()函数,计算字符串参考链接http://www.w3school.com.cn/js/jsref_eval.asp
function d(val){
var t=document.getElementById("t").value;
document.getElementById("t").value=eval(t);
document.getElementById("sDiv").innerHTML=t+val+eval(t);
}
//加法计算的方法也是用了eval()函数
function jia(val){
var t=document.getElementById("t").value;
if(t.indexOf("+") == -1&&t.indexOf("-") == -1&&t.indexOf("*") == -1&&t.indexOf("/") == -1){//防止多输两个计算符号,看前面是否有没有没计算的,只能进行一步计算,str.indexOf("xx")== -1,返回字符串第一次出现的位置,没有出现返回-1,以此判断。
	document.getElementById("t").value=t+val;
document.getElementById("sDiv").innerHTML=t+val;
}else{
document.getElementById("t").value=eval(t)+"+";
document.getElementById("sDiv").innerHTML=t+"+";
}


}
//减法
function jian(val){
var t=document.getElementById("t").value;
if(t.indexOf("+") == -1&&t.indexOf("-") == -1&&t.indexOf("*") == -1&&t.indexOf("/") == -1){
	document.getElementById("t").value=t+val;
document.getElementById("sDiv").innerHTML=t+val;
}else{
document.getElementById("t").value=eval(t)+"-";
document.getElementById("sDiv").innerHTML=t+"-";
}


}
//乘法
function cheng(val){
var t=document.getElementById("t").value;
if(t.indexOf("+") == -1&&t.indexOf("-") == -1&&t.indexOf("*") == -1&&t.indexOf("/") == -1){
	document.getElementById("t").value=t+val;
document.getElementById("sDiv").innerHTML=t+val;
}else{
document.getElementById("t").value=eval(t)+"*";
document.getElementById("sDiv").innerHTML=t+"*";
}


}
//除法
function chu(val){
var t=document.getElementById("t").value;
if(t.indexOf("+") == -1&&t.indexOf("-") == -1&&t.indexOf("*") == -1&&t.indexOf("/") == -1){
	document.getElementById("t").value=t+val;
document.getElementById("sDiv").innerHTML=t+val;
}else{
document.getElementById("t").value=eval(t)+"/";
document.getElementById("sDiv").innerHTML=t+"/";
}


}
  </script> 
  <div style="text-align:center ; margin-top:200px">
 
        <p>简单的计算器</p>
 
		<input type="text" name="" id="t" value="" readonly="readonly" style="width:235px;height:30px;border-radius: 5px;margin: 2px;" /><br/>
		<input type="button" name="" id="" value="1" onclick="calCulate(this.value)"/>
		<input type="button" name="" id="" value="2" onclick="calCulate(this.value)"/>
		<input type="button" name="" id="" value="3" onclick="calCulate(this.value)"/>
		
		<input type="button" name="" id="" value="+" onclick="jia(this.value)" style="color:red" />
		<br/>
		<input type="button" name="" id="" value="4" onclick="calCulate(this.value)"/>
		<input type="button" name="" id="" value="5" onclick="calCulate(this.value)"/>
		<input type="button" name="" id="" value="6" onclick="calCulate(this.value)"/>
		
		<input type="button" name="" id="" value="-" onclick="jian(this.value)" style="color:red" />
		<br/>
		<input type="button" name="" id="" value="7" onclick="calCulate(this.value)"/>
		<input type="button" name="" id="" value="8" onclick="calCulate(this.value)"/>
		<input type="button" name="" id="" value="9" onclick="calCulate(this.value)"/>
		
		<input type="button" name="" id="" value="*" onclick="cheng(this.value)" style="color:red" />
		<br/>
		<input type="button" name="" id="" value="." onclick="calCulate(this.value)"/>
		<input type="button" name="" id="" value="0" onclick="calCulate(this.value)"/>
		<input type="button" name="" id="" value="C" onclick="c()" style="color:red" />

		<input type="button" name="" id="" value="/" onclick="chu(this.value)" style="color:red" />
		<br/>
		<input type="button" name="" id="" value="=" onclick="d(this.value)" style="width:246px;color:red" />
		
		<br/>
		<p id="sDiv"></p>
		
  </div>
  
 </body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张枫大师哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值