js to implement web calculator

js to implement web calculator

How to make a simple web calculator using HTML, CSS and JS?

A computer has:

  • Computer overall frame
  • Input box
  • Input button

Computer overall frame:

/*Set div style*/
 #showdiv{
   border: solid 1px;
   border-radius: 5px;
   width: 350px;
   height: 400px;
   text-align: center;
   margin: auto;/*Set center*/
   margin-top: 50x;
   background-color: rgb(214, 219, 190);    
}

Input box:

/*Set the input box style*/
  input[type=text]{
      margin-top: 20px;
      width: 290px;
      height: 40px;
      font-size: 20px;

}

Input Button:

/*Set button style*/
   input[type=button]{
      width: 60px;
      height: 60px;
      margin-top: 20px;
      margin-left: 5px;
      margin-right: 5px;
      font-size: 30px;
      font-weight: bold;
      font-family: "楷书";
}

Use js code to perform corresponding business logic operations:

<!--Declare js code-->
        <script>
            function test(btn){
                //Get the button object var number = btn.value;
                //Execute the corresponding business logic switch (number) {
                    case "=":
                        document.getElementById("input").value = eval(document.getElementById("input").value);
                        break;
                    case "c":
                        document.getElementById("input").value="";
                        break;
                    default:
                        //Assign the value of the button to the input box document.getElementById("input").value+=number;
                        break;
                }
                
            }
</script>

Using HTML for computer typesetting:

<body>
    <div id="showdiv">
        <input type="text" id="input" readonly="readonly"><br>
        <input type="button" value="1" onclick="test(this)">
        <input type="button" value="2" onclick="test(this)">
        <input type="button" value="3" onclick="test(this)">
        <input type="button" value="4" onclick="test(this)"><br>
        <input type="button" value="5" onclick="test(this)">
        <input type="button" value="6" onclick="test(this)">
        <input type="button" value="7" onclick="test(this)">
        <input type="button" value="8" onclick="test(this)"><br>
        <input type="button" value="9" onclick="test(this)">
        <input type="button" value="+" onclick="test(this)">
        <input type="button" value="-" onclick="test(this)">
        <input type="button" value="*" onclick="test(this)"><br>
        <input type="button" value="0" onclick="test(this)">
        <input type="button" value="/" onclick="test(this)">
        <input type="button" value="c" onclick="test(this)">
        <input type="button" value="=" onclick="test(this)">
      


    </div>
</body>

Overall code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        /*Set div style*/
        #showdiv{
            border: solid 1px;
            border-radius: 5px;
            width: 350px;
            height: 400px;
            text-align: center;
            margin: auto;/*Set center*/
            margin-top: 50x;
            background-color: rgb(214, 219, 190);    
         }
        /*Set the input box style*/
        input[type=text]{
            margin-top: 20px;
            width: 290px;
            height: 40px;
            font-size: 20px;

        }
        /*Set button style*/
        input[type=button]{
            width: 60px;
            height: 60px;
            margin-top: 20px;
            margin-left: 5px;
            margin-right: 5px;
            font-size: 30px;
            font-weight: bold;
            font-family: "楷书";
        }
        </style>
        <!--Declare js code-->
        <script>
            function test(btn){
                //Get the button object var number = btn.value;
                //Execute the corresponding business logic switch (number) {
                    case "=":
                        document.getElementById("input").value = eval(document.getElementById("input").value);
                        break;
                    case "c":
                        document.getElementById("input").value="";
                        break;
                    default:
                        //Assign the value of the button to the input box document.getElementById("input").value+=number;
                        break;
                }
                
            }
        </script>
        
        
    <title>Document</title>
</head>
<body>
    <div id="showdiv">
        <input type="text" id="input" readonly="readonly"><br>
        <input type="button" value="1" onclick="test(this)">
        <input type="button" value="2" onclick="test(this)">
        <input type="button" value="3" onclick="test(this)">
        <input type="button" value="4" onclick="test(this)"><br>
        <input type="button" value="5" onclick="test(this)">
        <input type="button" value="6" onclick="test(this)">
        <input type="button" value="7" onclick="test(this)">
        <input type="button" value="8" onclick="test(this)"><br>
        <input type="button" value="9" onclick="test(this)">
        <input type="button" value="+" onclick="test(this)">
        <input type="button" value="-" onclick="test(this)">
        <input type="button" value="*" onclick="test(this)"><br>
        <input type="button" value="0" onclick="test(this)">
        <input type="button" value="/" onclick="test(this)">
        <input type="button" value="c" onclick="test(this)">
        <input type="button" value="=" onclick="test(this)">
      


    </div>
</body>
</html>

Result:

You must have learned how to make front-end web computers!

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • A simple calculator written in javascript, with a lot of content and practical methods. Recommended
  • Simple js code to realize calculator operation
  • js implements a simple calculator
  • HTML+JS implements simple calculator code (addition, subtraction, multiplication and division)
  • Simple calculator implementation code written in JS
  • javascript-simple calculator implementation step decomposition (with pictures)
  • js implements a simple calculator
  • Pure javascript code to implement calculator functions (three methods)
  • Simple implementation of js web calculator
  • Web calculator A JS calculator

<<:  VMWare virtual machine 15.X LAN network configuration tutorial diagram

>>:  Detailed explanation of the definition and function of delimiter in MySQL

Recommend

How to limit the input box to only input pure numbers in HTML

Limit input box to only pure numbers 1、onkeyup = ...

How to modify the MySQL character set

1. Check the character set of MySQL show variable...

VUE implements token login verification

This article example shares the specific code of ...

Detailed explanation of Vue's list rendering

Table of contents 1. v-for: traverse array conten...

Install Docker on CentOS 7

If you don't have a Linux system, please refe...

An example of elegant writing of judgment in JavaScript

Table of contents Preface 1. Monadic Judgment 1.1...

MySQL 20 high-performance architecture design principles (worth collecting)

Open Source Database Architecture Design Principl...

Use Nginx to build a streaming media server to realize live broadcast function

Written in front In recent years, the live stream...

How MLSQL Stack makes stream debugging easier

Preface A classmate is investigating MLSQL Stack&...

Detailed explanation of how to use the Vue license plate input component

A simple license plate input component (vue) for ...

HTML table markup tutorial (6): dark border color attribute BORDERCOLORDARK

In a table, you can define the color of the lower...

CSS3 gradient background compatibility issues

When we make a gradient background color, we will...

Analysis of the operating principle and implementation process of Docker Hub

Similar to the code hosting service provided by G...

Vue achieves seamless carousel effect

This article shares the specific code of Vue to a...