JS实现的简单四则运算计算器功能示例


Posted in Javascript onSeptember 27, 2017

本文实例讲述了JS实现的简单四则运算计算器功能。分享给大家供大家参考,具体如下:

先来看看运行效果:

JS实现的简单四则运算计算器功能示例

具体代码如下:

<!DOCTYPE html>
<html>
<meta name="content-type" content="text/html; charset=UTF-8">
<head>
  <title>3water.com 计算器 Calculator</title>
  <!--将按键内容以字符串形式存储在文字框中当按钮为“=”时,调用eval方法计算结果然后将结果输出文字框中-->
  <script type="text/javascript">
    var numresult;
    var str;
    function onclicknum(nums) {
      str = document.getElementById("nummessege");
      str.value = str.value + nums;
    }
    function onclickclear() {
      str = document.getElementById("nummessege");
      str.value = "";
    }
    function onclickresult() {
      str = document.getElementById("nummessege");
      numresult = eval(str.value);
      str.value = numresult;
    }
  </script>
</head>
<body bgcolor="affff" >
<!--定义按键表格,每个按键对应一个事件触发-->
<table border="1" align="center" bgColor="#bbff77"
    style="height: 350px; width: 270px">
  <tr>
    <td colspan="4">
      <input type="text" id="nummessege"
          style="height: 90px; width: 350px; font-size: 50px" />
    </td>
  </tr>
  <tr>
    <td>
      <input type="button" value="1" id="1" onclick="onclicknum(1)"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
    <td>
      <input type="button" value="2" id="2" onclick="onclicknum(2)"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
    <td>
      <input type="button" value="3" id="3" onclick="onclicknum(3)"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
    <td>
      <input type="button" value="+" id="add" onclick="onclicknum('+')"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
  </tr>
  <tr>
    <td>
      <input type="button" value="4" id="4" onclick="onclicknum(4)"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
    <td>
      <input type="button" value="5" id="5" onclick="onclicknum(5)"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
    <td>
      <input type="button" value="6" id="6" onclick="onclicknum(6)"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
    <td>
      <input type="button" value="-" id="sub" onclick="onclicknum('-')"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
  </tr>
  <tr>
    <td>
      <input type="button" value="7" id="7" onclick="onclicknum(7)"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
    <td>
      <input type="button" value="8" id="8" onclick="onclicknum(8)"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
    <td>
      <input type="button" value="9" id="9" onclick="onclicknum(9)"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
    <td>
      <input type="button" value="*" id="mul" onclick="onclicknum('*')"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
  </tr>
  <tr>
    <td colspan="2">
      <input type="button" value="0" id="0" onclick="onclicknum(0)"
          style="height: 70px; width: 190px; font-size: 35px">
    </td>
    <td>
      <input type="button" value="." id="point" onclick="onclicknum('.')"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
    <td>
      <input type="button" value="/" id="division"
          onclick="onclicknum('/')"
          style="height: 70px; width: 90px; font-size: 35px">
    </td>
  </tr>
  <tr>
    <td colspan="2">
      <input type="button" value="Del" id="clear"
          onclick="onclickclear()"
          style="height: 70px; width: 190px; font-size: 35px" />
    </td>
    <td colspan="2">
      <input type="button" value="=" id="result"
          onclick="onclickresult()"
          style="height: 70px; width: 190px; font-size: 35px" />
    </td>
  </tr>
</table>
</body>
</html>

PS:这里再为大家推荐几款计算工具供大家进一步参考借鉴:

在线一元函数(方程)求解计算工具:
http://tools.3water.com/jisuanqi/equ_jisuanqi

科学计算器在线使用_高级计算器在线计算:
http://tools.3water.com/jisuanqi/jsqkexue

在线计算器_标准计算器:
http://tools.3water.com/jisuanqi/jsq

希望本文所述对大家JavaScript程序设计有所帮助。

Javascript 相关文章推荐
javascript Demo模态窗口
Dec 06 Javascript
jquery插件实现鼠标经过图片右侧显示大图的效果(类似淘宝)
Feb 04 Javascript
JavaScript通过元素的ID和name设置样式
Jul 08 Javascript
你所不了解的javascript操作DOM的细节知识点(一)
Jun 17 Javascript
js点击列表文字对应该行显示背景颜色的实现代码
Aug 05 Javascript
深入理解javascript中concat方法
Dec 12 Javascript
jQuery实现的分页功能示例
Jan 22 Javascript
angular2中router路由跳转navigate的使用与刷新页面问题详解
May 07 Javascript
JQuery常用选择器功能与用法实例分析
Dec 23 jQuery
如何使用Jquery动态生成二级选项列表
Feb 06 jQuery
JS+css3实现幻灯片轮播图
Aug 14 Javascript
JS实现页面侧边栏效果探究
Jan 08 Javascript
Three.js利用顶点绘制立方体的方法详解
Sep 27 #Javascript
js实现扫雷小程序的示例代码
Sep 27 #Javascript
Three.js如何实现雾化效果示例代码
Sep 27 #Javascript
浅谈Angular4中常用管道
Sep 27 #Javascript
深入理解Vue.js源码之事件机制
Sep 27 #Javascript
js截取字符串功能的实现方法
Sep 27 #Javascript
详解node+express+ejs+bootstrap构建项目
Sep 27 #Javascript
You might like
PHP连接操作access数据库实例
2015/03/30 PHP
Yii2框架使用计划任务的方法
2016/05/25 PHP
PHP实现网页内容html标签补全和过滤的方法小结【2种方法】
2017/04/27 PHP
Laravel框架Request、Response及Session操作示例
2019/05/06 PHP
javascript Zifa FormValid 0.1表单验证 代码打包下载
2007/06/08 Javascript
jquery表单验证使用插件formValidator
2012/11/10 Javascript
jquery验证手机号码、邮箱格式是否正确示例代码
2013/07/28 Javascript
js动态设置鼠标事件示例代码
2013/10/30 Javascript
JS清除文本框内容离开在恢复及鼠标离开文本框时触发js的方法
2016/01/12 Javascript
JS/jQuery判断DOM节点是否存在的简单方法
2016/11/24 Javascript
Bootstrap源码学习笔记之bootstrap进度条
2016/12/24 Javascript
微信小程序的分类页面制作
2017/06/27 Javascript
JavaScript基础之流程控制语句的用法
2017/08/31 Javascript
JavaScript指定断点操作实例教程
2018/09/18 Javascript
vue实现信息管理系统
2020/05/30 Javascript
VSCode 配置uni-app的方法
2020/07/11 Javascript
js实现简单的点名器随机色实例代码
2020/09/20 Javascript
python解析json实例方法
2013/11/19 Python
Python学习小技巧之利用字典的默认行为
2017/05/20 Python
Python实现可设置持续运行时间、线程数及时间间隔的多线程异步post请求功能
2018/01/11 Python
python2.7到3.x迁移指南
2018/02/01 Python
flask框架视图函数用法示例
2018/07/19 Python
解决Python获取字典dict中不存在的值时出错问题
2018/10/17 Python
python中tkinter的应用:修改字体的实例讲解
2019/07/17 Python
python批量修改xml属性的实现方式
2020/03/05 Python
简单了解python调用其他脚本方法实例
2020/03/26 Python
Python3+selenium配置常见报错解决方案
2020/08/28 Python
C&A巴西网上商店:时尚、衣服、手机和鞋子
2020/06/07 全球购物
司机的工作范围及职责
2013/11/13 职场文书
电脑教师的自我评价
2013/12/18 职场文书
大学生优秀自荐信范文
2014/02/25 职场文书
模具设计与制造专业自荐书
2014/07/01 职场文书
社保转移委托书范本
2014/10/08 职场文书
党的群众路线教育实践活动领导班子整改措施
2014/10/28 职场文书
整改通知书
2015/04/20 职场文书
oracle覆盖导入dmp文件的2种方法
2021/05/21 Oracle