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 相关文章推荐
非常不错的功能强大代码简单的管理菜单美化版
Jul 09 Javascript
js tab 选项卡
Apr 26 Javascript
网页运行时提示对象不支持abigimage属性或方法
Aug 10 Javascript
JavaScript字符串常用类使用方法汇总
Apr 14 Javascript
js控制网页前进和后退的方法
Jun 08 Javascript
Javascript原型链的原理详解
Jan 05 Javascript
JS实现把鼠标放到链接上出现滚动文字的方法
Apr 06 Javascript
Bootstrap实现各种进度条样式详解
Apr 13 Javascript
JS实现将二维数组转为json格式字符串操作示例
Jul 12 Javascript
Node.js 获取微信JS-SDK CONFIG的方法示例
May 21 Javascript
使用jquery-easyui的布局layout写后台管理页面的代码详解
Jun 19 jQuery
详细介绍Next.js脚手架完整搭建封装
Apr 26 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
2020最新CPU的性能排名
2020/04/02 数码科技
PHP+ajaxfileupload+jcrop插件完美实现头像上传剪裁
2014/06/09 PHP
微信支付PHP SDK ―― 公众号支付代码详解
2016/09/13 PHP
基于jquery的滚动新闻列表
2010/06/19 Javascript
High Performance JavaScript(高性能JavaScript)读书笔记分析
2011/05/05 Javascript
跨域请求之jQuery的ajax jsonp的使用解惑
2011/10/09 Javascript
jquery遍历数组与筛选数组的方法
2013/11/05 Javascript
jquery中each遍历对象和数组示例
2014/08/05 Javascript
javascript中var的重要性分析
2015/02/11 Javascript
JavaScript简单判断复选框是否选中及取出值的方法
2015/08/13 Javascript
javascript如何实现暂停功能
2015/11/06 Javascript
javascript Slip.js实现整屏滑动的手机网页
2015/11/25 Javascript
详解javascript实现瀑布流列式布局
2016/01/29 Javascript
jquery实现简单Tab切换菜单效果
2020/07/17 Javascript
JavaScript使用ZeroClipboard操作剪切板
2017/05/10 Javascript
JS实现求数组起始项到终止项之和的方法【基于数组扩展函数】
2017/06/13 Javascript
JavaScript中常见的八个陷阱总结
2017/06/28 Javascript
vue实现Excel文件的上传与下载功能的两种方式
2019/06/28 Javascript
vue滚动tab跟随切换效果
2020/06/29 Javascript
解决vue单页面 回退页面 keeplive 缓存问题
2020/07/22 Javascript
node.js 如何监视文件变化
2020/09/01 Javascript
python提取字典key列表的方法
2015/07/11 Python
Windows系统下PhantomJS的安装和基本用法
2018/10/21 Python
python读取word文档,插入mysql数据库的示例代码
2018/11/07 Python
Pytorch 扩展Tensor维度、压缩Tensor维度的方法
2020/09/09 Python
德国户外商店:eXXpozed
2020/07/25 全球购物
三八妇女节超市活动方案
2014/08/18 职场文书
平面设计师岗位职责
2014/09/18 职场文书
劳模先进事迹材料
2014/12/24 职场文书
给上级领导的感谢信
2015/01/22 职场文书
社区党员干部承诺书
2015/05/04 职场文书
离婚纠纷代理词
2015/05/23 职场文书
小兵张嘎观后感300字
2015/06/03 职场文书
Python 如何解决稀疏矩阵运算
2021/05/26 Python
mysql事务隔离级别详情
2021/10/24 MySQL
Spring中的使用@Async异步调用方法
2021/11/01 Java/Android