jquery简易版在线计算器插件

所属分类: 网页特效-实用工具    2023-08-12 04:18:23

jquery简易版在线计算器插件 ie兼容6
反馈问题  查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

jquery简易版在线计算器插件(共8个文件)

    • main.js
    • index.html
    • style.css

使用方法

var num1 = null; 
var num2 = null; 
var operator = null;
var total = 0;
var screenDisplay = '';
var numPeriod = 0;

$(document).ready(function() {
  $('#clear').on('click', function () {
    reset()
    displayScreen(total);
  });

  $('.digit').on('click', function (e) { 
    handleDigit(e);
  });

  $('.decPoint').on('click', function (e) {
    // Only add the decimal point if there is none present
    if (numPeriod == 0) {
      handleDigit(e);
      numPeriod++;
    }
  })

  $('.operation').on('click', function (e) {
    if (num1 == null) {
      return;
    } else if (num2 == null) {
      operator = e.target.id;
      displayScreen(num1 + operator);
      console.log({num1, operator, num2, total})
    } else {
      /* If both num1 and num2 are full, then push the 
      existing value to num1 and save the operator */
      num1 = compute(num1, num2);
      operator = e.target.id;
      num2 = null;
      displayScreen(num1 + operator);
      // console.log({num1, operator, num2, total})
    }
  });

  $('.equal').on('click', function (e) {
    if (num1) {
      if (!operator) {
        total = num1;
        displayScreen(num1);
        // console.log({num1, operator, num2, total})
        return;
      }
    }

    total = compute(num1, num2);
    displayScreen(total);

    operator = null;
    num1 = total;
    num2 = null;
  });
});

function compute(stringA, stringB) {
  let a = parseFloat(stringA);
  let b = parseFloat(stringB);

  switch (operator) {
    case "/":
      return (a / b).toFixed(3);
    case "-":
      return (a - b).toFixed(3);
    case "+":
      return (a + b).toFixed(3);
    case "*":
      return (a * b).toFixed(3);
    default:
      break;
  }
}

function displayScreen(text) {
  $('.screen').text(text);
  screenDisplay = text.toString();
}

function handleDigit(e) {
  if (num1 == null) {
    num1 = e.target.value;
    displayScreen(num1);
    // console.log({num1, operator, num2, total})
  } else if (operator == null) {
      num1 += e.target.value;
      displayScreen(num1);
      // console.log({num1, operator, num2, total})
  } else {
    if (num2 == null) {
      num2 = e.target.value;
      displayScreen(num1 + operator + num2);
      // console.log({num1, operator, num2, total})
    } else {
      num2 += e.target.value;
      displayScreen(num1 + operator + num2);
      // console.log({num1, operator, num2, total})
    }
  }
}

function reset() {
  num1 = null;
  num2 = null;
  operator = null;
  total = 0;
  numPeriod = 0;
}

站长提示:
1. 平台上所有素材资源,需注册登录会员方能正常下载。
2. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励20K币
3. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
4. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 实用工具

基于javascript实现检测浏览器名称和版本参数信息

这是一款获取访客用户浏览器版本信息工具,它还支持Chrome和Firefox浏览器兼容性检查,以便提醒用户获得更流畅的浏览体验。
  实用工具
 193  

jquery按原子序数排序的标准元素周期表

使用jQuery、JSON和CSSGrid创建一个按原子序数排序的标准元素周期表(118个元素),鼠标悬停即可显示详细信息。
  实用工具
 173  

基于javascript实现的JPG图片转PNG图片在线工具

一款在线图片格式转换器实用工具,可将任意格式图片转换成png格式图片。先上传本地一张图片,然后点击转换按钮,即可快速转换成功,带下载png图片功能。
  实用工具
 600  

javascript实现特定div内容打印

一款页面指定区域内容打印插件,printDiv方法将id为“printableTable”的div的内容复制到隐藏的iframe中,然后触发打印对话框。此功能有助于在不需要打开新窗口的情况下,能够轻松打印网页中的指定内容。
  实用工具
 248  

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论