js html实现计算器功能

 更新时间:2018年11月13日 17:01:44   作者:~李疆  
这篇文章主要为大家详细介绍了js html实现计算器功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了html实现计算器功能的具体代码,供大家参考,具体内容如下

代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title></title>
  <script type="text/javascript">
 
    var numresult;
    var str;
    function onclicknum(nums)
    {
      str = document.getElementById("nummessege");
      str.value = str.value + nums;
    }
    function onclickclear()
    {
      str = document.getElementById("nummessege");
      str.value = "";
    }
    function onclickresult()
    {
      str = document.getElementById("nummessege");
      numresult = eval(str.value);
      str.value = numresult;
    }
  </script>
 
</head>
<body bgcolor="affff">
  <!--定义按键表格,每个按键对应一个事件触发-->
  <table border="1" align="center" bgColor="#bbff77"
      style="height: 350px; width: 270px">
    <tr>
      <td colspan="4">
        <input type="text" id="nummessege"
            style="height: 90px; width: 350px; font-size: 50px" />
      </td>
    </tr>
    <tr>
      <td>
        <input type="button" value="1" id="1" onclick="onclicknum(1)"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
      <td>
        <input type="button" value="2" id="2" onclick="onclicknum(2)"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
      <td>
        <input type="button" value="3" id="3" onclick="onclicknum(3)"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
      <td>
        <input type="button" value="+" id="add" onclick="onclicknum('+')"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
    </tr>
    <tr>
      <td>
        <input type="button" value="4" id="4" onclick="onclicknum(4)"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
      <td>
        <input type="button" value="5" id="5" onclick="onclicknum(5)"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
      <td>
        <input type="button" value="6" id="6" onclick="onclicknum(6)"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
      <td>
        <input type="button" value="-" id="sub" onclick="onclicknum('-')"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
    </tr>
    <tr>
      <td>
        <input type="button" value="7" id="7" onclick="onclicknum(7)"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
      <td>
        <input type="button" value="8" id="8" onclick="onclicknum(8)"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
      <td>
        <input type="button" value="9" id="9" onclick="onclicknum(9)"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
      <td>
        <input type="button" value="*" id="mul" onclick="onclicknum('*')"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <input type="button" value="0" id="0" onclick="onclicknum(0)"
            style="height: 70px; width: 190px; font-size: 35px">
      </td>
      <td>
        <input type="button" value="." id="point" onclick="onclicknum('.')"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
      <td>
        <input type="button" value="/" id="division"
            onclick="onclicknum('/')"
            style="height: 70px; width: 90px; font-size: 35px">
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <input type="button" value="Del" id="clear"
            onclick="onclickclear()"
            style="height: 70px; width: 190px; font-size: 35px" />
      </td>
      <td colspan="2">
        <input type="button" value="=" id="result"
            onclick="onclickresult()"
            style="height: 70px; width: 190px; font-size: 35px" />
      </td>
    </tr>
  </table>
</body>
</html>

效果图

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

相关文章

  • JavaScript页面加载事件实例讲解

    JavaScript页面加载事件实例讲解

    在本篇文章中我们给大家通过实例讲解一下关于JavaScript页面加载事件的相关知识点内容,有需要的朋友们可以参考下。
    2019-09-09
  • JS实现图片旋转动画效果封装与使用示例

    JS实现图片旋转动画效果封装与使用示例

    这篇文章主要介绍了JS实现图片旋转动画效果封装与使用,结合实例形式分析了JavaScript实现图片元素旋转的相关功能代码的封装与使用操作技巧,需要的朋友可以参考下
    2018-07-07
  • 你可能不知道的前端算法之文字避让(inMap)

    你可能不知道的前端算法之文字避让(inMap)

    这篇文章主要给大家介绍了关于前端算法之文字避让的相关资料,对于这个知识相信很多的朋友都不知道,但看到效果会惊叹不已,实现这一个效果主要利用的是inMap文字避让功能,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
    2018-01-01
  • Bootstrap实现水平排列的表单

    Bootstrap实现水平排列的表单

    这篇文章主要为大家详细介绍了Bootstrap实现水平排列的表单的相关代码,代码简洁,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • JavaScript实现焦点进入文本框内关闭输入法的核心代码

    JavaScript实现焦点进入文本框内关闭输入法的核心代码

    这篇文章给大家分享js实现焦点进入文本框内关闭输入法,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-09-09
  • javascript实现缓动动画效果

    javascript实现缓动动画效果

    这篇文章主要为大家详细介绍了javascript实现缓动动画效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • three.js中文文档学习之如何本地运行详解

    three.js中文文档学习之如何本地运行详解

    这篇文章主要给大家介绍了关于three.js中文文档学习之如何在本地运行的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-11-11
  • 详解JavaScript添加给定的标签选项

    详解JavaScript添加给定的标签选项

    这篇文章给大家分享了JavaScript实现添加给定的标签选项的相关知识点内容以及代码实例,有兴趣的朋友们参考下。
    2018-09-09
  • JavaScript如何调用C++模块中的函数

    JavaScript如何调用C++模块中的函数

    这篇文章主要给大家介绍了关于JavaScript如何调用C++模块中函数的相关资料,文中通过代码介绍的非常详细,对大家的学习或者工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-01-01
  • js判断对象是否存在某个属性的几种办法技巧

    js判断对象是否存在某个属性的几种办法技巧

    这篇文章主要给大家介绍了关于js判断对象是否存在某个属性的几种办法技巧,判断对象中是否有某属性的常见方式总结,不同的场景要使用不同的方式,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-12-12

最新评论