源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JavaScript实现简单的四则运算计算器完整实例

  • 时间:2020-03-02 09:06 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JavaScript实现简单的四则运算计算器完整实例
本文实例讲述了JavaScript实现简单的四则运算计算器。分享给大家供大家参考,具体如下: 运行效果图如下: [img]http://files.jb51.net/file_images/article/201704/2017428105019135.gif?2017328105045[/img] 完整实例代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>computer</title>
  <script>
  function compute(){
    str1=Number(document.getElementById("txt1").value);
    str2=Number(document.getElementById("txt2").value);
    comp=document.getElementById("select").value;
    var result;
    switch(comp) {
      case "+":
        comp=str1+str2;
        break;
      case "-":
        comp=str1-str2;
        break;
      case "*":
        comp=str1*str2;
        break;
      case "/":
        if(str2==0){
          alert("除数不能为0!");
          comp='';
        }else{
          comp=str1/str2;
        }
        break;
    }
    document.getElementById("result").value=comp;
  }
  </script>
</head>
<body>
  <input type="text" id="txt1"/>
  <select name="method" id="select">
    <option value="+">+</option>
    <option value="-">-</option>
    <option value="*">*</option>
    <option value="/">/</option>
  </select>
  <input type="text" id="txt2"/>
  <input type="button" id="b1" value="等于" onclick="compute()" />
  <input type="text" id="result"/>
</body>
</html>

[b]PS:这里再为大家推荐几款计算工具供大家进一步参考借鉴:[/b] [b]在线一元函数(方程)求解计算工具: [/b][url=http://tools.jb51.net/jisuanqi/equ_jisuanqi]http://tools.jb51.net/jisuanqi/equ_jisuanqi[/url] [b]科学计算器在线使用_高级计算器在线计算: [/b][url=http://tools.jb51.net/jisuanqi/jsqkexue]http://tools.jb51.net/jisuanqi/jsqkexue[/url] [b]在线计算器_标准计算器: [/b][url=http://tools.jb51.net/jisuanqi/jsq]http://tools.jb51.net/jisuanqi/jsq[/url] 更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/119.htm]JavaScript数学运算用法总结[/url]》、《[url=http://www.1sucai.cn/Special/297.htm]JavaScript数据结构与算法技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/278.htm]JavaScript数组操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/148.htm]JavaScript排序算法总结[/url]》、《[url=http://www.1sucai.cn/Special/281.htm]JavaScript遍历算法与技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/472.htm]JavaScript查找算法技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/439.htm]JavaScript错误与调试技巧总结[/url]》 希望本文所述对大家JavaScript程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部