Home > Web Front-end > JS Tutorial > body text

HTML+js implements simple calculator code (addition, subtraction, multiplication and division)

高洛峰
Release: 2017-01-20 17:20:05
Original
5602 people have browsed it

html+js implements simple calculator code (addition, subtraction, multiplication and division)

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<table>
  <tr>
   <td><input type="button" value="add"   onclick="setOp(&#39;+&#39;, &#39;add&#39;);"/></td>
   <td><input type="button" value="miner" onclick="setOp(&#39;-&#39;, &#39;miner&#39;);"/></td>
   <td><input type="button" value="times" onclick="setOp(&#39;*&#39;, &#39;times&#39;);"/></td>
   <td><input type="button" value="divide" onclick="setOp(&#39;/&#39;, &#39;divide&#39;);"/></td>
  </tr>
</table>
<table id="tb_calc" style="display:none;">
   <tr>
    <td> <input id="x" type="text" style="width:100px" value="" name="x" /></td>
    <td> <lable id="op" name="op"></lable> </td>
    <td> <input id="y" type="text" style="width:100px" value="" name="y" /> </td>
    <td> <input id="opTips" type="button" value="" onclick="calc();"/> </td>
    <td> <lable id="z" name="z"></lable> </td>
  </tr>
</table>
<script type="application/javascript">
  function setOp(op, opTips)
  {
    var tb=document.getElementById("tb_calc");
    tb.style.display = "none";
           
    document.getElementById("x").value = ""; 
    document.getElementById("y").value = ""; 
    document.getElementById("z").innerText = ""; 
    document.getElementById("op").innerText = op;
    document.getElementById("opTips").value = opTips;
     
    tb.style.display = "block";
  }
  function calc()
  {
    var x = parseInt(document.getElementById("x").value); 
    var y = parseInt(document.getElementById("y").value);
    var op = document.getElementById("op").innerText;
     
    var z = "";
    switch(op)
    {
      case &#39;+&#39;:
        z = x + y;
        break;
      case &#39;-&#39;:
        z = x - y;
        break;
      case &#39;*&#39;: ;
        z = x * y;
        break;
      case &#39;/&#39;: ;
        z = x / y;
        break;
      default:
        z = &#39;&#39;;
    }
    console.log(x, op, y, &#39;=&#39;, z);
    document.getElementById("z").innerText = z;
  }
</script>
</body>
</html>
Copy after login

The screenshot is as follows:

HTML+js implements simple calculator code (addition, subtraction, multiplication and division)

The above article is simple to implement with html+js The calculator code (addition, subtraction, multiplication and division) is all the content shared by the editor. I hope it can give you a reference, and I also hope you will pay more attention to the PHP Chinese website.

For more html+js implementation of simple calculator code (addition, subtraction, multiplication and division) related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template