JavaScript制作简易计算器(不用eval)
分类:计算机教程

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

本文实例为大家分享了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>

代码如下

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

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>简易计算器</title>
 <style type="text/css">
  *{
   margin:0;
   padding: 0;
  }
  .content{
   width: 300px;
   height: 400px;
   margin: 50px auto;
   background: #CBC6C6;
   border:1px solid #139EB7;
   box-shadow: -5px -5px 5px #565454;
   border-radius: 15px;
  }
  #toptext{
   width: 280px;
   height: 50px;
   margin: 10px auto;
   margin-left: 9px;
   line-height: 50px;
   border-radius: 15px;
   text-align: right;
   font-size: 24px;
   border: 1px #F0D711 solid;
  }
  .btns{
   width: 280px;
   height: 300px;
   margin: 10px auto;
  }
  .btns input{
   width: 50px;
   margin: 10px;
   height: 60px;
   float: left;
   font-size: 24px;
   line-height: 60px;
   border-radius: 10px;
   transform-origin: left top;
   background: linear-gradient(to right,#0df60d,red);
  }
  .btns input:hover{
   background: linear-gradient(to left,#0df60d,red);
   transform: rotate(15deg);
  }
 </style>
</head>
<body>
 <div class="content">
  <input type="text" id="toptext">
  <div class="btns">
   <input type="button" value="1" class="num">
   <input type="button" value="2" class="num">
   <input type="button" value="3" class="num">
   <input type="button" value=" " class="jisuan">
   <input type="button" value="4" class="num">
   <input type="button" value="5" class="num">
   <input type="button" value="6" class="num">
   <input type="button" value="-" class="jisuan">
   <input type="button" value="7" class="num">
   <input type="button" value="8" class="num">
   <input type="button" value="9" class="num">
   <input type="button" value="*" class="jisuan">
   <input type="button" value="0" class="num">
   <input type="button" value="C" id="clear">
   <input type="button" value="=" id="equals">
   <input type="button" value="/" class="jisuan">
  </div>
 </div>
 <script type="text/javascript">
  var text = document.getElementsByTagName('input')[0];
  var clear = document.getElementById('clear');
  var equals = document.getElementById('equals');
  var js = document.getElementsByClassName('jisuan');
  var num = document.getElementsByClassName('num');

  text.value = 0;
  var tex = "";
  var fuhao = "";
  var firstNum = "";
  var secondNum = "";

  for(var i=0 ; i<num.length; i  ){
   num[i].onclick = function (){
    if(fuhao){
     text.value  = this.value;
     secondNum = this.value;
    }else{
     tex = tex this.value;
     firstNum = tex;
     text.value = tex;
    }
   };
  }
  for(var j=0; j<js.length; j  ){
   js[j].onclick = function(){
    fuhao = this.value;
    text.value  = this.value; 
   };  
  }

  equals.onclick=function(){
   switch (fuhao) {
    case ' ':
     var res = firstNum*1   secondNum*1;
     break;
    case '-':
     var res = firstNum - secondNum;
     break;
    case '*':
     var res = firstNum * secondNum;
     break;
    case '/':
     var res = firstNum / secondNum;
     break;   
    default:
     break;
   }
   text.value = res;
   tex = "";
   fuhao = "";
   firstNum = "";
   secondNum = "";
  }
  clear.onclick = function(){
   text.value = "";
  }
 </script>
</body>
</html>

您可能感兴趣的文章:

界面:

本文由美洲杯赔率发布于计算机教程,转载请注明出处:JavaScript制作简易计算器(不用eval)

上一篇:如何在PHP中使用正则表达式进行查找替换 下一篇:没有了
猜你喜欢
热门排行
精彩图文
  • JavaScript制作简易计算器(不用eval)
    JavaScript制作简易计算器(不用eval)
    本文实例为大家分享了js制作简易计算器的具体代码,供大家参考,具体内容如下 本文实例为大家分享了js简易计算器的具体代码,供大家参考,具体内容
  • 探讨如何在php168_cms中提取验证码
    探讨如何在php168_cms中提取验证码
    复制代码 代码如下: 复制代码 代码如下: ?php function yzImg($nmsg){  if (function_exists('imagecreatetruecolor')){   $imstr[0]["s"]=$nmsg[0];   $imstr[1]["s"]=$nmsg[1];   $imstr[2]["s
  • 浅析SVN常见问题及解决方法
    浅析SVN常见问题及解决方法
    问好(新加入的资源): --这说明该文件是项目中新增文件资源,新增资源可以是文件、图片、代码等。 LINK Address : http://blog.csdn.net/niu_hao/article/details/6774471 米
  • 详解Vue.js入门环境搭建
    详解Vue.js入门环境搭建
    vue这个新的工具,确实能够提高效率,在经历的一段时间的摧残之后,终于能够有一个系统的认识了,下面就今天的收获做一个总结,也是vue入门的精髓:
  • javascript 中的继承实例详解,javascript实例男篮世
    javascript 中的继承实例详解,javascript实例男篮世
    javascript 中的继承实例详解 javascript 中的继承实例详解,javascript实例 javascript 中的继承实例详解 阅读目录 原型链继承 借用构造函数 组合继承 寄生组合式