Home > Article > Web Front-end > How to find the difference in javascript
Javascript method to achieve difference: 1. Create an HTML sample file; 2. Create an input input box; 3. Calculate the difference of the numbers in the input through the "num1-num2" formula, and then put it in a div. Can.
The operating environment of this article: windows7 system, javascript version 1.8.5, DELL G3 computer
How to find the difference in javascript?
JavaScript calculates the difference between two numbers:
Requirements
Enter two numbers in the two input boxes and click When pressing the button, the difference between the two numbers is calculated and displayed in the div with the id of result.
Implementation code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> body{ padding-top: 100px; text-align: center; } #result{ width: 50%; height: 100px; margin: 0 auto; border: 1px solid #ccc; } </style> </head> <body> <input type="text" id="ipt1"/> <input type="text" id="ipt2"/> <button id="btn">计算</button> <div class="result"></div> <script type="text/javascript"> // 得到input里面的值,然后拿到减,把结果放到div中 // input.value div.innerHTML 事件绑定 // 找对象 input button div var oIpt1 = document.getElementById("ipt1") var oIpt2 = document.getElementById("ipt2") var oBtn = document.getElementById("btn") var oBox = document.getElementById("result") // 事件函数绑定 oBtn.onclick = function(){ // 计算input里面数字的差 然后放到div中 var num1 = oIpt1.value var num2 = oIpt2.value var result = num1-num2 // 给div设置一个内容 result oBox.innerHTML = result } </script> </body> </html>
Recommended study: "javascript basic tutorial"
The above is the detailed content of How to find the difference in javascript. For more information, please follow other related articles on the PHP Chinese website!