Home >Web Front-end >JS Tutorial >How to implement addition, subtraction, multiplication and division in javascript

How to implement addition, subtraction, multiplication and division in javascript

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-13 18:15:088335browse

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.

How to implement addition, subtraction, multiplication and division in javascript

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(&#39;a&#39;).value;
    var b=document.getElementById(&#39;b&#39;).value;
    var c=document.getElementById(&#39;c&#39;);
    c.value=Number(a)+Number(b);
}
function jian(){//减法
    var a=document.getElementById(&#39;a&#39;).value;
    var b=document.getElementById(&#39;b&#39;).value;
    var c=document.getElementById(&#39;c&#39;);
    c.value=Number(a)-Number(b);
}
function cheng(){//乘法
    var a=document.getElementById(&#39;a&#39;).value;
    var b=document.getElementById(&#39;b&#39;).value;
    var c=document.getElementById(&#39;c&#39;);
    c.value=Number(a)*Number(b);
}
function chu(){//除法
    var a=document.getElementById(&#39;a&#39;).value;
    var b=document.getElementById(&#39;b&#39;).value;
    var c=document.getElementById(&#39;c&#39;);
    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!

Statement:
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