1. parseFloat() function Make a simple calculator on a web page, enter two numbers in the text box, and be able to add, subtract, multiply and divide the two numbers. 2. JavaScript global properties
3. Complete code<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script type="text/javascript"> function cal(){ var nums = document.getElementsByName("num"); var sz = document.getElementsByName("js"); var result = document.getElementsByName("rs"); var n1 = parseFloat(nums[0].value); var n2 = parseFloat(nums[1].value); /*parseFloat() function parses a string and returns a floating point number. This function specifies whether the first character in a string is a number. If it is, the string is parsed until the end of the number is reached and the number is returned as a number. instead of as a string. */ switch(sz[0].value){ case "add" : result[0].value = n1 + n2; break; case "min" : result[0].value = n1 - n2; break; case "mul" : result[0].value = n1 * n2; break; case "div" : result[0].value = n1/n2; break; } } </script> </head> <body> <div align="center"> <input type="text" name="num" value="" onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" /> <select name="js" size="1"> <option value="add">+</option> <option value="min">-</option> <option value="mul">*</option> <option value="div">/</option> </select> <input type="text" name="num" value=""onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" /> = <input type="text" name="rs" value=""/><br> <button id="btn"onclick="cal()">Calculate</button> </div> </body> </html> Effect display: This is the end of this article about how to add, subtract, multiply and divide two numbers by inputting them in html. For more relevant html addition, subtraction, multiplication and division content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! |
Table of contents The use of Vue's keep-alive...
Basics 1. Use scaffolding to create a project and...
HTML comments, we often need to make some HTML co...
A few days ago, I found that the official version...
MySQL regular sorting, custom sorting, and sortin...
I. Introduction 1: SSL Certificate My domain name...
Detailed example of database operation object mod...
There are currently three ways to display the cen...
XML/HTML CodeCopy content to clipboard < div c...
Why do we achieve this effect? In fact, this ef...
css-vars-ponyfill When using CSS variables to ach...
Docker Hub official website 1. Search for Python ...
How to delete the container created in Docker 1. ...
Development Trends: html (Hypertext Markup Languag...
This article shares the specific code of the js n...