JavaScript编写简单的计算器


Posted in Javascript onNovember 25, 2015

本文实例讲述了JavaScript编写简单计算器的代码。分享给大家供大家参考。具体如下:
运行效果截图如下:

JavaScript编写简单的计算器

具体代码如下

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>计算器</title>
 <style>
  /*Basic reset*/
*{
 margin:0;
 padding:0;
 box-sizing: border-box;
 font: 14px Arial,sans-serif;
}
html{
 height:100%;
 background-color:lightslategrey;
}

#calculator{
 margin: 15px auto;
 width:330px;
 height:400px;
 border: 1px solid lightgray;
 background-color:darkgrey;
 padding:15px;
}

/*LOGO*/
.LOGO{
 height:20px;

}
.LOGO .name{
 float:left;
 line-height:30px;
}
.LOGO .verson{
 float:right;
 line-height:30px;
}
/*screen*/
#shuRu{
 margin-top:15px;
}
.screen{
 margin-top:5px;
 width:300px;
 height:40px;
 text-align: right;
 padding-right:10px;
 font-size:20px;
}
#keys{
 border:1px solid lightgray;
 height:223px;
 margin-top:25px;
 padding:8px;
}
#keys .last{
 margin-right:0px;
}
.footer{
 margin-top:20px;
 height:20px;
}
.footer .link{
 float:right;
}

#keys .buttons{
 float:left;
 width: 42px;
 height: 36px;
 text-align:center;
 background-color:lightgray;
 margin: 0 17px 20px 0;
}
 </style>
</head>
<body>
<div id="calculator">
 <div class="LOGO">
  <span class="name">简单的计算器</span>
  <span class="verson">@walker</span>
 </div>
 <div id="shuRu">
  <!--screen输入栏-->
  <div class="screen">
   <input type="text" id="screenName" name="screenName" class="screen">
  </div>
 </div>
 <div id="keys">
  <!-- j -->
  <!--第一排-->
  <input type="button" id="7" onclick="jsq(this.id)" value="7" class="buttons">
  <input type="button" id="8" onclick="jsq(this.id)" value="8" class="buttons">
  <input type="button" id="9" onclick="jsq(this.id)" value="9" class="buttons">
  <input type="button" id="Back" onclick="tuiGe()" value="Back" class="buttons">
  <input type="button" id="C" onclick="clearNum()" value="C" class="buttons" style="margin-right:0px">
  <!--第二排-->
  <input type="button" id="4" onclick="jsq(this.id)" value="4" class="buttons">
  <input type="button" id="5" onclick="jsq(this.id)" value="5" class="buttons">
  <input type="button" id="6" onclick="jsq(this.id)" value="6" class="buttons">
  <input type="button" id="*" onclick="jsq(this.id)" value="X" class="buttons">
  <input type="button" id="/" onclick="jsq(this.id)" value="/" class="buttons" style="margin-right:0px">
  <!--第三排-->
  <input type="button" id="1" onclick="jsq(this.id)" value="1" class="buttons">
  <input type="button" id="2" onclick="jsq(this.id)" value="2" class="buttons">
  <input type="button" id="3" onclick="jsq(this.id)" value="3" class="buttons">
  <input type="button" id="+" onclick="jsq(this.id)" value="+" class="buttons">
  <input type="button" id="-" onclick="jsq(this.id)" value="-" class="buttons" style="margin-right:0px">
  <!--第四排-->
  <input type="button" id="0" onclick="jsq(this.id)" value="0" class="buttons">
  <input type="button" id="00" onclick="jsq(this.id)" value="00" class="buttons">
  <input type="button" id="." onclick="jsq(this.id)" value="." class="buttons">
  <input type="button" id="%" onclick="jsq(this.id)" value="%" class="buttons">
  <input type="button" id="eva" onclick="eva()" value="=" class="buttons" style="margin-right:0px">
 </div>
 <div class="footer">
  <span class="aside">欢迎使用JavaScript计算器</span>
   <span class="link">
    <a href="#" title="声明" target="_blank">反馈</a>
   </span>
 </div>
</div>
</body>
</html>

js代码:

<script>
 var num = 0; // 定义第一个输入的数据
 function jsq(num) {
  //获取当前输入
  if(num=="%"){
   document.getElementById('screenName').value=Math.round(document.getElementById('screenName').value)/100;
  }else{
   document.getElementById('screenName').value += document.getElementById(num).value;
  }
 }
 function eva() {
  //计算输入结果
  document.getElementById("screenName").value = eval(document.getElementById("screenName").value);
 }
 function clearNum() {
  //清0
  document.getElementById("screenName").value = null;
  document.getElementById("screenName").focus();
 }
 function tuiGe() {
  //退格
  var arr = document.getElementById("screenName");
  arr.value = arr.value.substring(0, arr.value.length - 1);
 }
</script>

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

一个简单的计算器就是这样实现的,大家也可以利用javascript编写计算器,,希望对大家的学习有所帮助。

Javascript 相关文章推荐
[原创]静态页面也可以实现预览 列表不同的显示方式
Oct 14 Javascript
My Desktop :) 桌面式代码
Dec 29 Javascript
精心挑选的15款优秀jQuery 本特效插件和教程
Aug 06 Javascript
JS实现鼠标箭头变成一个燃烧烛光效果的方法
Feb 28 Javascript
JS实现的仿东京商城菜单、仿Win右键菜单及仿淘宝TAB特效合集
Sep 28 Javascript
AngularJS自动表单验证
Feb 01 Javascript
JS中多步骤多分步的StepJump组件实例详解
Apr 01 Javascript
BOM之navigator对象和用户代理检测
Feb 10 Javascript
jQuery中 bind的用法简单介绍
Feb 13 Javascript
bootstrap精简教程_动力节点Java学院整理
Jul 14 Javascript
JavaScript 跨域之POST实现方法
May 07 Javascript
vue富文本编辑器组件vue-quill-edit使用教程
Sep 21 Javascript
HTML5 Shiv完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
Nov 25 #Javascript
jQuery插件formValidator自定义函数扩展功能实例详解
Nov 25 #Javascript
jQuery实现鼠标双击Table单元格变成文本框及输入内容后更新到数据库的方法
Nov 25 #Javascript
基于jQuery实现响应式圆形图片轮播特效
Nov 25 #Javascript
jquery+css实现动感的图片切换效果
Nov 25 #Javascript
基于jQuery实现拖拽图标到回收站并删除功能
Nov 25 #Javascript
基于jquery实现页面滚动时顶部导航显示隐藏
Apr 20 #Javascript
You might like
YB217、YB235、YB400浅听
2021/03/02 无线电
建立良好体验度的Web注册系统ajax
2007/07/09 Javascript
JQUERY 实现窗口滚动搜索框停靠效果(类似滚动停靠)
2013/03/27 Javascript
引入JS文件IE6报语法错误或缺少对象问题的解决方法
2014/01/09 Javascript
jQuery设置Cookie及删除Cookie实例分析
2016/04/15 Javascript
原生javascript移动端滑动banner效果
2017/03/10 Javascript
详解AngularJS脏检查机制及$timeout的妙用
2017/06/19 Javascript
seajs实现强制刷新本地缓存的方法分析
2017/10/16 Javascript
微信小程序 如何引入外部字体库iconfont的图标
2018/01/31 Javascript
vuejs实现ready函数加载完之后执行某个函数的方法
2018/08/31 Javascript
微信小程序实现下拉菜单切换效果
2020/03/30 Javascript
微信小程序实现翻牌抽奖动画
2020/09/21 Javascript
[53:18]Spirit vs Liquid Supermajor小组赛A组 BO3 第三场 6.2
2018/06/03 DOTA
简单的Python的curses库使用教程
2015/04/11 Python
浅谈Python生成器generator之next和send的运行流程(详解)
2017/05/08 Python
Python中matplotlib中文乱码解决办法
2017/05/12 Python
python matplotlib绘图,修改坐标轴刻度为文字的实例
2018/05/25 Python
对python捕获ctrl+c手工中断程序的两种方法详解
2018/12/26 Python
python Jupyter运行时间实例过程解析
2019/12/13 Python
深入浅析Python 函数注解与匿名函数
2020/02/24 Python
python多进程 主进程和子进程间共享和不共享全局变量实例
2020/04/25 Python
瑞典领先的汽车零部件网上零售商:bildelaronline24.se
2017/01/12 全球购物
UML设计模式笔试题
2014/06/07 面试题
护士求职推荐信范文
2013/11/23 职场文书
自我评价格式
2014/01/06 职场文书
爱国卫生月实施方案
2014/02/21 职场文书
社区矫正工作方案
2014/06/04 职场文书
2014年小学重阳节活动策划方案
2014/09/16 职场文书
2014县委书记四风对照检查材料思想汇报
2014/09/21 职场文书
“四风”问题自我剖析材料思想汇报
2014/09/23 职场文书
2014乡镇领导班子四风对照检查材料思想汇报
2014/10/05 职场文书
诉讼授权委托书
2014/10/15 职场文书
党风廉政教育心得体会2016
2016/01/22 职场文书
银行柜员工作心得体会
2016/01/23 职场文书
详解盒子端CSS动画性能提升
2021/05/24 HTML / CSS
Centos7中MySQL数据库使用mysqldump进行每日自动备份的编写
2021/08/02 MySQL