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 相关文章推荐
各种常用浏览器getBoundingClientRect的解析
May 21 Javascript
一段批量给页面上的控件赋值js
Jun 19 Javascript
javascript onmouseout 解决办法
Jul 17 Javascript
解析Jquery取得iframe中元素的几种方法
Jul 04 Javascript
jQuery中ajax和post处理json的不同示例对比
Nov 02 Javascript
jQuery中:lt选择器用法实例
Dec 29 Javascript
JS实现的网页背景闪电闪烁效果代码
Oct 17 Javascript
js检测用户输入密码强度
Oct 22 Javascript
BootStrap下的弹出框加载select2框架失败的解决方法
Aug 31 Javascript
JavaScript实现修改伪类样式
Nov 27 Javascript
vue select二级联动第二级默认选中第一个option值的实例
Jan 10 Javascript
小程序实现抽奖动画
Apr 16 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
substr()函数中文版
2006/10/09 PHP
php中static静态变量的使用方法详解
2010/06/04 PHP
php去掉URL网址中带有PHPSESSID的配置方法
2014/07/08 PHP
PHP多文件上传实例
2015/07/09 PHP
php 静态属性和静态方法区别详解
2017/04/09 PHP
Yii框架实现的验证码、登录及退出功能示例
2017/05/20 PHP
php实现获取农历(阴历)、节日、节气的类与用法示例
2017/11/20 PHP
JavaScript中实现块作用域的方法
2010/04/01 Javascript
分享Javascript中最常用的55个经典小技巧
2013/11/29 Javascript
js 上下左右键控制焦点(示例代码)
2013/12/14 Javascript
教你快速搭建Node.Js服务器的方法教程
2017/03/30 Javascript
JavaScript中document.referrer的用法详解
2017/07/04 Javascript
详解如何构建Angular项目目录结构
2017/07/13 Javascript
详谈构造函数加括号与不加括号的区别
2017/10/26 Javascript
angular 实现同步验证器跨字段验证的方法
2019/04/11 Javascript
Vue+Koa2 打包后进行线上部署的教程详解
2019/07/31 Javascript
node.js中Buffer缓冲器的原理与使用方法分析
2019/11/23 Javascript
解决vue打包 npm run build-test突然不动了的问题
2020/11/13 Javascript
比较详细Python正则表达式操作指南(re使用)
2008/09/06 Python
python统计文本字符串里单词出现频率的方法
2015/05/26 Python
django.db.utils.ProgrammingError: (1146, u“Table‘’ doesn’t exist”)问题的解决
2018/07/13 Python
pyinstaller打包多个py文件和去除cmd黑框的方法
2019/06/21 Python
Python 用三行代码提取PDF表格数据
2019/10/13 Python
python3 常见解密加密算法实例分析【base64、MD5等】
2019/12/19 Python
python GUI库图形界面开发之PyQt5窗口布局控件QStackedWidget详细使用方法
2020/02/27 Python
德国最大的服装、鞋子和配件在线商店之一:Outfits24
2019/07/23 全球购物
意大利折扣和优惠券网站:Groupalia
2019/10/09 全球购物
应届生求职信写作技巧
2013/10/24 职场文书
婚庆公司的创业计划书
2014/01/22 职场文书
授权委托书样本
2014/04/03 职场文书
2014年十一国庆节爱国演讲稿
2014/09/23 职场文书
2014年政府采购工作总结
2014/12/09 职场文书
求职信范文怎么写
2015/03/19 职场文书
红高粱观后感
2015/06/10 职场文书
Vue h函数的使用详解
2022/02/18 Vue.js
实例详解Python的进程,线程和协程
2022/03/13 Python