简单快速的实现js计算器功能


Posted in Javascript onAugust 17, 2017

在js的全局方法中有一个eval()方法,由于平时不怎么用,所以到关键时候就没想起来它

想写一个简易的计算器,本来以为要不了多久就能写出来的,谁知道愣是花费了我近两个小时的时间来写,但结果还是不能令我满意。想找一个更好的方法来写,不想写的那么麻烦,用什么方法呢?想了一个遍,后来猛然看到屏幕上有一个eval(),当时我的电脑正打开着网页。福星来了,对啊,我浪费了这么长时间写出来的东西还不能令我满意,一个eval()不就搞定了么,下面就给大家看一下我写的代码,写的很粗糙,还请大家多多指正。

<!DOCTYPE html> 
<html> 
 <head> 
 <meta charset="UTF-8"> 
 <title></title> 
 <style type="text/css"> 
  .box{ 
  width: 400px; 
  height: 450px; 
  margin: 0 auto; 
  background: pink; 
  } 
  .show{ 
  width:100%; 
  height: 100px; 
  line-height: 100px; 
  text-align: right; 
  background: #FFFFFF; 
  border:1px solid #000; 
  } 
  .operate{ 
  width: 100%; 
  height: 250px; 
  margin-top: 50px; 
  } 
  p{ 
  margin-top: 20px; 
  text-align: center; 
  } 
  input{ 
  width: 80px; 
  height: 40px; 
  /*margin-left: 50px;*/ 
  text-align: center; 
  } 
  input:nth-child(5n),input:first-child{ 
  margin-left: 0; 
  } 
 </style> 
 </head> 
 <body> 
 <div class="box"> 
  <div class="show" id="show"></div> 
  <div class="operate"> 
  <p> 
   <input type="button" name="one" id="one" value="1" onclick="num(1)"/> 
   <input type="button" name="two" id="two" value="2" onclick="num(2)"/> 
   <input type="button" name="three" id="three" value="3" onclick="num(3)"/> 
   <input type="button" name="plus" id="plus" value="+" onclick="num('+')"/> 
  </p> 
  <p> 
   <input type="button" name="four" id="four" value="4" onclick="num(4)"/> 
   <input type="button" name="five" id="five" value="5" onclick="num(5)"/> 
   <input type="button" name="six" id="six" value="6" onclick="num(6)"/> 
   <input type="button" name="reduce" id="reduce" value="-" onclick="num('-')"/> 
  </p> 
  <p> 
   <input type="button" name="seven" id="seven" value="7" onclick="num(7)"/> 
   <input type="button" name="eight" id="eight" value="8" onclick="num(8)"/> 
   <input type="button" name="nine" id="nine" value="9" onclick="num(9)"/> 
   <input type="button" name="times" id="times" value="x" onclick="num('*')"/> 
  </p> 
  <p> 
   <input type="button" name="divide" id="divide" value="/" onclick="num(/)"/> 
   <input type="button" name="zero" id="zero" value="0" onclick="num(0)"/> 
   <input type="button" name="equal" id="equal" value="=" onclick="equal()"/> 
   <input type="button" name="mod" id="mod" value="%" onclick="num('%')"/> 
  </p> 
  </div> 
   
 </div> 
 </body> 
 <script type="text/javascript"> 
 function num(figure){ 
//  var figure = figure.toString(); 
  var show = document.getElementById("show"); 
  showNum = show.innerHTML+figure; 
  show.innerHTML = showNum; 
 } 
 function equal(){ 
  document.getElementById("show").innerHTML = eval(document.getElementById("show").innerHTML); 
  /*var lal = document.getElementById("show").innerHTML; 
  alert(lal);*/ 
 } 
 </script> 
</html>

关于计算器的精彩文章请查看《计算器专题》 ,更多精彩等你来发现!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript单例模式的简单实现方法
Jul 25 Javascript
使用JavaScript脚本判断页面是否在微信中被打开
Mar 06 Javascript
jQuery3.0中的buildFragment私有函数详解
Aug 16 Javascript
对比分析Django的Q查询及AngularJS的Datatables分页插件
Feb 07 Javascript
WebSocket实现简单客服聊天系统
May 12 Javascript
微信小程序实现拖拽 image 触摸事件监听的实例
Aug 17 Javascript
Angular实现点击按钮控制隐藏和显示功能示例
Dec 29 Javascript
JS+HTML5实现获取手机验证码倒计时按钮
Aug 08 Javascript
vue组件之间的数据传递方法详解
Apr 19 Javascript
vue-cli单页面预渲染seo-prerender-spa-plugin操作
Aug 10 Javascript
解决新建一个vue项目过程中遇到的问题
Oct 22 Javascript
微信小程序接入vant Weapp组件的详细步骤
Oct 28 Javascript
使用jquery的jsonp如何发起跨域请求及其原理详解
Aug 17 #jQuery
如何理解Vue的.sync修饰符的使用
Aug 17 #Javascript
浅谈JS中的反柯里化( uncurrying)
Aug 17 #Javascript
js编写简单的聊天室功能
Aug 17 #Javascript
Angular使用 ng-img-max 调整浏览器中的图片的示例代码
Aug 17 #Javascript
Canvas放置反弹效果随机图形(实例)
Aug 17 #Javascript
js实现方块上下左右移动效果
Aug 17 #Javascript
You might like
PHP Stream_*系列函数
2010/08/01 PHP
PHP和Mysqlweb应用开发核心技术-第1部分 Php基础-2 php语言介绍
2011/07/03 PHP
解析linux下安装memcacheq(mcq)全过程笔记
2013/06/27 PHP
twig模板获取全局变量的方法
2016/02/05 PHP
Laravel5.1 框架Request请求操作常见用法实例分析
2020/01/04 PHP
『jQuery』名称冲突使用noConflict方法解决
2013/04/22 Javascript
Javascript简单实现可拖动的div
2013/10/22 Javascript
javascript加减乘除的简单实例
2016/07/12 Javascript
用原生js统计文本行数的简单示例
2016/08/19 Javascript
element ui分页多选,翻页记忆的实例
2019/09/03 Javascript
layui使用templet格式化表格数据的方法
2019/09/16 Javascript
jquery实现吸顶导航效果
2020/01/08 jQuery
[15:39]教你分分钟做大人:龙骑士
2014/10/30 DOTA
[01:33:25]DOTA2-DPC中国联赛 正赛 Elephant vs IG BO3 第一场 1月24日
2021/03/11 DOTA
Python模拟登录验证码(代码简单)
2016/02/06 Python
Python判断值是否在list或set中的性能对比分析
2016/04/16 Python
怎样使用Python脚本日志功能
2016/08/14 Python
Python虚拟环境virtualenv的安装与使用详解
2017/05/28 Python
python数据类型_元组、字典常用操作方法(介绍)
2017/05/30 Python
Python编程实现控制cmd命令行显示颜色的方法示例
2017/08/14 Python
Python将多个excel文件合并为一个文件
2018/01/03 Python
pytorch 共享参数的示例
2019/08/17 Python
PHP基于phpqrcode类库生成二维码过程解析
2020/05/28 Python
如何使用pycharm连接Databricks的步骤详解
2020/09/23 Python
Does C# support multiple inheritance? (C#支持多重继承吗)
2012/01/04 面试题
测绘工程个人的自我评价
2013/11/23 职场文书
2014年两会学习心得体会
2014/03/17 职场文书
学校安全生产承诺书
2014/05/23 职场文书
教师个人发展总结
2015/02/11 职场文书
2015秋季小学开学寄语
2015/05/27 职场文书
地道战观后感400字
2015/06/04 职场文书
就业推荐表院系意见
2015/06/05 职场文书
立春观后感
2015/06/18 职场文书
Nginx设置HTTPS的方法步骤 443证书配置方法
2022/03/21 Servers
OpenCV项目实践之停车场车位实时检测
2022/04/11 Python
Vue操作Storage本地化存储
2022/04/29 Vue.js