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! |
This article shares the specific code of JavaScri...
Due to your company standards, you may only allow...
What is an index? Why create an index? Indexes ar...
1. Traditional binlog master-slave replication, s...
Web front end 1 Student ID Name gender age 01 Zha...
Introduction By enabling the slow query log, MySQ...
When laying out the page, in order to give users ...
Perfect solution to VMware black screen after Mac...
1. Import echart in HTML file <!-- Import echa...
Download the official website First go to the off...
Sometimes you will see English commas ",&quo...
1. nohup Run the program in a way that ignores th...
Table of contents 1. Modify by binding the style ...
The usage format of the mysqladmin tool is: mysql...
clear:both is used to清除浮動This is the impression I...