JavaScript制作简易计算器(不用eval)

 更新时间:2017年02月05日 10:19:16   作者:万花果子  
这篇文章主要为大家详细介绍了JavaScript制作简易计算器的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了js制作简易计算器的具体代码,供大家参考,具体内容如下

<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <style type="text/css">
  * {
   padding: 0;
   margin: 0;
  }
  li {
   list-style: none;
  }
  body {
   background: #940032;
  }

  #counter {
   width: 500px;
   height: 420px;
   background: #939;
   margin: 50px auto 0;
   position: relative;
  }

  #counter h2 {
   line-height: 42px;
   padding-left: 15px;
   font-size: 14px;
   font-family: arial;
   color: #ff3333;
  }

  #counter a {
   font-weight: normal;
   text-decoration: none;
   color: #ff3333;
  }

  #counter a:hover {
   text-decoration: underline;
  }

  #bg {
   width: 280px;
   height: 200px;
   border: 3px solid #680023;
   background: #990033;
   filter: alpha(opacity=80);
   opacity: 0.8;
   position: absolute;
   left: 50%;
   top: 115px;
   margin-left: -141px;
  }

  #counter_content {
   width: 250px;
   position: absolute;
   top: 130px;
   left: 130px;
   z-index: 1;
  }

  #counter_content h3 {
   margin-bottom: 10px;
  }

  #counter_content h3 input {
   border: none;
   width: 223px;
   height: 30px;
   line-height: 30px;
   padding: 0 10px;
   background: url(img/ico.png) no-repeat;
   text-align: right;
   color: #333;
   font-size: 14px;
   font-weight: bold;
  }

  #counter_content div {
   width: 250px;
  }

  #counter_content input {
   width: 60px;
   height: 30px;
   line-height: 30px;
   float: left;
   background: url(img/ico.png) no-repeat -303px 0;
   text-align: center;
   color: #fff;
   cursor: pointer;
   margin: 0 1px 4px 0;
   border: 0;
  }

  #counter_content div > input:hover {
   background: url(img/ico.png) no-repeat -243px 0;
  }

  #counter p {
   width: 500px;
   position: absolute;
   bottom: 20px;
   left: 0;
   color: #ff3333;
   text-align: center;
   font-size: 12px;
  }
 </style>
</head>
<body>
<div id="counter">
 <h2>简易计算</h2>
 <div id="counter_content">
  <h3><input id="input1" type="text" value="0"/></h3>
  <div id="div1">
   <input type="button" value="7" onclick="kick('7')"/>
   <input type="button" value="8" onclick="kick('8')"/>
   <input type="button" value="9" onclick="kick('9')"/>
   <input type="button" value="+" onclick="kick('+')"/>
   <input type="button" value="4" onclick="kick('4')"/>
   <input type="button" value="5" onclick="kick('5')"/>
   <input type="button" value="6" onclick="kick('6')"/>
   <input type="button" value="-" onclick="kick('-')"/>
   <input type="button" value="1" onclick="kick('1')"/>
   <input type="button" value="2" onclick="kick('2')"/>
   <input type="button" value="3" onclick="kick('3')"/>
   <input type="button" value="*" onclick="kick('*')"/>
   <input type="button" value="0" onclick="kick('0')"/>
   <input type="button" value="C" onclick="kick('C')"/>
   <input type="button" value="=" onclick="kick('=')"/>
   <input type="button" value="/" onclick="kick('/')"/>
  </div>
 </div>
</div>
</body>
<script>
 var showInput = document.getElementById("input1");
 var isClear = false;
 var tempStr = "";
 var clacType = "";
 var isContinue = true;
 function kick(clickValue) {
  switch (clickValue) {
   case "=":
    if (tempStr != "" && clacType != "") {
     showInput.value = clac(tempStr, showInput.value, clacType);
     isContinue = false;
     clacType = "";
    }
    break;
   case "+":
   case "-":
   case "*":
   case "/":
    //如果预存的操作符不为空 表示表示连续操作
    if (clacType != "" && !isContinue) { //先执行计算
     tempStr = clac(tempStr, showInput.value, clacType);
     isClear = true;
     clacType = clickValue;
    } else {
     tempStr = showInput.value; //点击操作符之后 预存字符
     isClear = true;//表示点击了操作符
     clacType = clickValue;//预存操作符
    }
    isContinue = true;
    break;
   case "C":
    showInput.value = "0";
    isClear = false;
    tempStr = "";
    clacType = "";
    break;
   default://普通的数字按钮点击
    showInput.value = showInput.value == "0" ? "" : showInput.value;
    isContinue = false;
    if (isClear) {
     showInput.value = "";
     showInput.value += clickValue;
     isClear = false;
    } else {
     showInput.value += clickValue;
    }
    break;
  }
 }


 function clac(num1, num2, type) {
  switch (type) {
   case "+":
    return Number(num1) + Number(num2);
   case "-":
    return Number(num1) - Number(num2);
   case "*":
    return Number(num1) * Number(num2);
   case "/":
    return Number(num1) / Number(num2);
   default:
    break;
  }
  }
 </script>

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

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

相关文章

  • 聊聊JS ES6中的解构

    聊聊JS ES6中的解构

    这篇文章主要介绍了JS ES6中的解构,对解构感兴趣的同学,可以参考下
    2021-04-04
  • JavaScript实现新年倒计时效果

    JavaScript实现新年倒计时效果

    这篇文章主要为大家详细介绍了JavaScript实现新年倒计时效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-11-11
  • js获取元素相对窗口位置的实现代码

    js获取元素相对窗口位置的实现代码

    这篇文章主要介绍了js获取元素相对窗口位置的实现代码,需要的朋友可以参考下
    2014-09-09
  • javascript模拟post提交隐藏地址栏的参数

    javascript模拟post提交隐藏地址栏的参数

    想要隐藏地址栏的参数,就只能用javascript模拟post提交,下面是示例代码,需要的朋友可以看看
    2014-09-09
  • 基于openlayers4实现点的扩散效果

    基于openlayers4实现点的扩散效果

    这篇文章主要为大家详细介绍了基于openlayers4实现点的扩散效果 ,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Bootstrap表格制作代码

    Bootstrap表格制作代码

    这篇文章主要为大家详细介绍了Bootstrap表格的制作代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Uploadify上传文件方法

    Uploadify上传文件方法

    Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示。接下来通过本文给大家介绍Uploadify上传文件方法,涉及到Uploadify在Aspnet中的使用相关知识,本文介绍的非常详细,具有参考借鉴价值,需要的朋友一起学习吧
    2016-03-03
  • 交叉观察器 IntersectionObserver用法详解

    交叉观察器 IntersectionObserver用法详解

    这篇文章主要为大家介绍了交叉观察器 IntersectionObserver用法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • js设置组合快捷键/tabindex功能的方法

    js设置组合快捷键/tabindex功能的方法

    本文主要介绍用js设置tabindex功能和js设置组合快捷键的方法,很简单,这样可以增强用户体验,方法就在下面
    2013-11-11
  • 微信小程序实现瀑布流布局与无限加载的方法详解

    微信小程序实现瀑布流布局与无限加载的方法详解

    瀑布流布局是我们日常开发中经常见到的一种页面布局方式,下面这篇文章主要给大家介绍了微信小程序实现瀑布流布局与无限加载的相关资料,文中给出了详细介绍和示例代码供大家参考学习,需要的朋友们下面来一起看看吧。
    2017-05-05

最新评论