Home >Web Front-end >JS Tutorial >How to implement addition, subtraction, multiplication and division in javascript
Javascript method to implement addition, subtraction, multiplication and division: first use "document.getElementById('id').value" to get the value of two numbers; 2. Use " ", "-", "*" and "/ "Operation symbols perform arithmetic operations and obtain results.
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <title>加减乘除运算</title> </head> <body> <h1 align="center">加减乘除运算</h1> <div id="" align="center"> 数值A:<input type="number" id="a" /> </br> 数值B:<input type="number" id="b" /> </br> 结果:<input type ="number" id="c"> </br> <input type="button" value="加" οnclick="jia()"> <input type="button" value="减" οnclick="jian()" > <input type="button" value="乘" οnclick="cheng()"> <input type="button" value="除" οnclick="chu()"> </div> </body> <script type="text/javascript"> function jia(){//加法 var a=document.getElementById('a').value; var b=document.getElementById('b').value; var c=document.getElementById('c'); c.value=Number(a)+Number(b); } function jian(){//减法 var a=document.getElementById('a').value; var b=document.getElementById('b').value; var c=document.getElementById('c'); c.value=Number(a)-Number(b); } function cheng(){//乘法 var a=document.getElementById('a').value; var b=document.getElementById('b').value; var c=document.getElementById('c'); c.value=Number(a)*Number(b); } function chu(){//除法 var a=document.getElementById('a').value; var b=document.getElementById('b').value; var c=document.getElementById('c'); c.value=Number(a)/Number(b); } </script> </html>
Recommended learning: javascript video tutorial
The above is the detailed content of How to implement addition, subtraction, multiplication and division in javascript. For more information, please follow other related articles on the PHP Chinese website!