js之简易计算器
来源:互联网 发布:网站授权码系统源码 编辑:程序博客网 时间:2024/05/22 10:58
用js实现简易计算器,附有详细代码。可以实现简单的数学算术运算,但是还是觉得里面有点小bag,接下来我会继续完善的。如果您有什么意见和建议,希望留言告诉我
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title> <style> *{margin: 0; padding: 0;} td{width: 80px;height:80px;} tr:nth-child(2) td{height:50px;} input[type="text"]{width: 340px;height: 82px;text-align: right; font-size: 30px;line-height: 82px} input[type="button"]{width: 84px;height: 84px;text-align: center; font-size: 30px;line-height: 82px} tr:nth-child(2) input[type="button"]{width: 170px;height: 50px;text-align: center; font-size: 30px;line-height: 52px} </style></head><body> <table border="1" cellpadding="0" cellspacing="0"> <tr> <td colspan="4"><input type="text" id="btn" value="" disabled/></td> </tr> <tr> <td colspan="2" height="50"><input type="button" class="btn" value="C"/></td> <td colspan="2" ><input type="button" class="btn" value="D"/></td> </tr> <tr> <td><input type="button" class="btn" value="7"/></td> <td><input type="button" class="btn" value="8"/></td> <td><input type="button" class="btn" value="9"/></td> <td><input type="button" class="btn" value="/"/></td> </tr> <tr> <td><input type="button" class="btn" value="4"/></td> <td><input type="button" class="btn" value="5"/></td> <td><input type="button" class="btn" value="6"/></td> <td><input type="button" class="btn" value="*"/></td> </tr> <tr> <td><input type="button" class="btn" value="1"/></td> <td><input type="button" class="btn" value="2"/></td> <td><input type="button" class="btn" value="3"/></td> <td><input type="button" class="btn" value="-"/></td> </tr> <tr> <td><input type="button" class="btn" value="0"/></td> <td><input type="button" class="btn" value="."/></td> <td><input type="button" class="btn" value="+"/></td> <td><input type="button" class="btn" value="="/></td> </tr> </table> <script> window.onload=function(){ var btn =document.getElementsByClassName("btn"); var result=document.getElementById("btn"); var arr=[]; for(var i=0;i<btn.length;i++){ btn[i].onclick=function(){ if(!isNaN(this.value)||this.value=="."){//只能输入数字和小数点if(result.value.indexOf(" ")==-1){}else result.value="";if(result.value.indexOf(".")==-1){//判断小数点的个数 result.value+=this.value; }else{ if(this.value!="."){ result.value+=this.value; } } } if(this.value=="+"||this.value=="-"||this.value=="*"||this.value=="/"){//将数字和运算符号存入数组中,方便运算。 arr[arr.length]=result.value; arr[arr.length]=this.value; result.value=""; // console.log(arr[0]) if(arr[arr.length-3]=="+"||arr[arr.length-3]=="-"||arr[arr.length-3]=="*"||arr[arr.length-3]=="/"){ arr.splice(arr.length-3,2) } if(arr[0]==""&&arr[1]=="+"||arr[0]==""&&arr[1]=="-"||arr[0]==""&&arr[1]=="*"||arr[0]==""&&arr[1]=="/"){ arr.splice(0,1,"0") } } if(this.value=="="){//计算 var str=""; arr[arr.length]=result.value; console.log(arr); for(var a in arr){ str+=arr[a]; } result.value=eval(str); arr=[]; } if(this.value=="C"){//清空 result.value=""; arr=[]; } if(this.value=="D"){//删除最后一个数字 result.value= result.value.substr(0,result.value.length-1) } } } } </script></body></html>
阅读全文
0 0
- js之简易计算器
- js实现简易计算器
- 简易的JS计算器
- Js简易计算器
- js实现简易计算器
- 简易js计算器
- js制作简易计算器
- js网页简易计算器
- Qt之简易计算器
- JAVA之简易计算器
- JS版简易网页计算器
- 用js实现简易计算器
- js编写的简易计算器
- js css 实现简易计算器
- 机试html+js简易计算器
- html+js简易计算器2
- js实现简易计算器功能
- 简易html+css+js计算器
- Mac 用户切换
- C语言基础-fgets/fputs/gets函数使用示例
- NYOJ:25-A Famous Music Composer
- 原系统改造的思维扩张之路---交易洪峰的应对
- ArcGIS水文分析实战教程(3)DEM数据准备
- js之简易计算器
- 创建对话框寻找多个面
- C语言中有关二维数组的指针问题
- 平面设计和UI设计的区别有哪些?
- 1201 18岁生日
- hotplug_uevent机制
- http状态响应码
- Windows Security
- VScode 插件的那些事