JS实现基本的网页计算器功能示例


Posted in Javascript onJanuary 16, 2020

本文实例讲述了JS实现基本的网页计算器功能。分享给大家供大家参考,具体如下:

<html>
    <head>
        <title>网页计算机</title>
        <meta charset="UTF-8"/>
        <style type="text/css">
            #jsjdiv{
                border: solid 1px black;
                border-radius: 5px;
                width: 200px;
                /*height: 400px;*/
                text-align: center; /*设置div内部居中*/
                margin: auto;    /*设置计算机居中*/
                background-color: darkgrey;
            }
            input[type=text]{
                width: 190px;          /*设置大小*/
                height: 35px;
                margin-top: 10px;   /*设置边框*/
                margin-bottom: 5px;
            }
            input[type=button]{
                width: 44px;
                height: 44px;
                /*margin-left: 5px;
                margin-right: 5px;*/
                margin-top: 5px;
                margin-bottom: 10px;
                font-size: 25px;  /*设置text的字体大小及深度*/
                font-weight: 600;
            }
        </style>
        <script type="text/javascript">
            function cal(btn){
                var num=btn.value;
                switch (num){   // 利用eval可以把string的内容转化成代码,在代码中输入可以直接进行计算
                    case "=":
                        document.getElementById("inp").value=eval(document.getElementById("inp").value);
                        break;
                    case "c":
                        document.getElementById("inp").value="";
                        break;
                    default:        //进行输入数据的拼接
                        document.getElementById("inp").value=document.getElementById("inp").value + num;
                        break;
                }
            }
        </script>
    </head>
    <body>
        <div id="jsjdiv">
            <input type="text" name="" id="inp" value="" /><br />
            <input type="button" name="" id="btn" value="1" onclick="cal(this)"/>
            <input type="button" name="" id="" value="2" onclick="cal(this)"/>
            <input type="button" name="" id="" value="3" onclick="cal(this)"/>
            <input type="button" name="" id="" value="4" onclick="cal(this)"/><br />
            <input type="button" name="" id="" value="5" onclick="cal(this)"/>
            <input type="button" name="" id="" value="6" onclick="cal(this)"/>
            <input type="button" name="" id="" value="7" onclick="cal(this)"/>
            <input type="button" name="" id="" value="8" onclick="cal(this)"/><br />
            <input type="button" name="" id="" value="9" onclick="cal(this)"/>
            <input type="button" name="" id="" value="+" onclick="cal(this)"/>
            <input type="button" name="" id="" value="-" onclick="cal(this)"/>
            <input type="button" name="" id="" value="*" onclick="cal(this)"/><br />
            <input type="button" name="" id="" value="0" onclick="cal(this)"/>
            <input type="button" name="" id="" value="/" onclick="cal(this)"/>
            <input type="button" name="" id="" value="c" onclick="cal(this)"/>
            <input type="button" name="" id="" value="=" onclick="cal(this)" />
        </div>
    </body>
</html>

运行效果:

JS实现基本的网页计算器功能示例

网页计算机:

利用css进行div的布局设置基本的计算机的基本的框架,

在其内部设置text进行显示,利用button添加按钮。

一个主要的点:我们要在按按钮的时候,把数据输出到text文本上。我们利用了function添加一个函数,在进行按按钮时,利用onclick,连接到函数,在函数中实现文本的显示。但是我们在函数中只能对某个id进行调用,这样就表示有多少按钮就要有多少函数,而且内容相同。所以我们引用了this(当前对象)进行调用。

另一方面,我们要实现计算,我们利用eval()把其中的内容转化为代码,就相当于代码执行。所以可以直接进行运算输出。

当我们输入“=”和“c"就要进行计算操作,相应的我们利用了switch进行区分。

感兴趣的朋友可以使用在线HTML/CSS/JavaScript前端代码调试运行工具:http://tools.3water.com/code/WebCodeRun测试上述代码运行效果。

Javascript 相关文章推荐
JavaScript 节点操作 以及DOMDocument属性和方法
Dec 06 Javascript
基于javascript制作微博发布栏效果
Apr 04 Javascript
javascript HTML5 Canvas实现圆盘抽奖功能
Apr 11 Javascript
深入浅析Extjs中store分组功能的使用方法
Apr 20 Javascript
浅谈jquery上下滑动的注意事项
Oct 13 Javascript
原生js实现查询天气小应用
Dec 09 Javascript
神级程序员JavaScript300行代码搞定汉字转拼音
May 20 Javascript
使用AngularJS对表单提交内容进行验证的操作方法
Jul 12 Javascript
vue-content-loader内容加载器的使用方法
Aug 05 Javascript
vue-router懒加载速度缓慢问题及解决方法
Nov 25 Javascript
微信小程序实现工作时间段选择
Feb 15 Javascript
vue中配置scss全局变量的步骤
Dec 28 Vue.js
JS数组进阶示例【数组的几种函数用法】
Jan 16 #Javascript
js实现简单的秒表
Jan 16 #Javascript
JS 数组基本用法入门示例解析
Jan 16 #Javascript
js实现上下左右键盘控制div移动
Jan 16 #Javascript
vue-cli3 取消eslint校验代码的解决办法
Jan 16 #Javascript
jQuery实现数字华容道小游戏(实例代码)
Jan 16 #jQuery
js实现点击生成随机div
Jan 16 #Javascript
You might like
PHP简单获取上月、本月、近15天、近30天的方法示例
2017/07/03 PHP
JavaScript面向对象编程
2008/03/02 Javascript
JavaScript 组件之旅(一)分析和设计
2009/10/28 Javascript
JavaScript flash复制库类 Zero Clipboard
2011/01/17 Javascript
Javascript 面向对象(二)封装代码
2012/05/23 Javascript
jquery插件制作 提示框插件实现代码
2012/08/17 Javascript
javascript级联下拉列表实例代码(自写)
2013/05/10 Javascript
chrome浏览器不支持onmouseleave事件的解决技巧
2013/05/31 Javascript
node.js中的buffer.Buffer.isBuffer方法使用说明
2014/12/14 Javascript
javascript实现依次输入input自动定焦
2014/12/23 Javascript
jQuery遮罩层实现方法实例详解(附遮罩层插件)
2015/12/08 Javascript
Node.js的MongoDB驱动Mongoose基本使用教程
2016/03/01 Javascript
浅谈JS函数定义方式的区别
2016/10/30 Javascript
用file标签实现多图文件上传预览
2017/02/14 Javascript
小程序Scroll-view上拉滚动刷新数据
2020/06/21 Javascript
[06:07]刀塔密之二:攻之吾命受之吾幸
2014/07/03 DOTA
phpsir 开发 一个检测百度关键字网站排名的python 程序
2009/09/17 Python
python映射列表实例分析
2015/01/26 Python
python单元测试unittest实例详解
2015/05/11 Python
Python2和Python3中urllib库中urlencode的使用注意事项
2018/11/26 Python
ubuntu 18.04 安装opencv3.4.5的教程(图解)
2019/11/04 Python
Python多继承以及MRO顺序的使用
2019/11/11 Python
python 上下文管理器及自定义原理解析
2019/11/19 Python
如何使用python实现模拟鼠标点击
2020/01/06 Python
全网首秀之Pycharm十大实用技巧(推荐)
2020/04/27 Python
Python tkinter实现简单加法计算器代码实例
2020/05/13 Python
pytorch  网络参数 weight bias 初始化详解
2020/06/24 Python
美国设计师精美珠宝购物网:Netaya
2016/08/28 全球购物
法国高保真音响和家庭影院商店:Son Video
2019/04/26 全球购物
中专毕业生自我鉴定范文
2013/11/09 职场文书
卫生巾广告词
2014/03/18 职场文书
2014年教研活动总结范文
2014/04/26 职场文书
美德少年事迹材料500字
2014/08/19 职场文书
门面房租房协议书
2014/08/20 职场文书
高三语文教学反思
2016/02/16 职场文书
2019职场实习报告该怎么写?
2019/07/01 职场文书