| <body> | |
| 请输入第一个数<input type="text" id="first"><br /><br /> | |
| 请输入第二个数<input type="text" id="second"><br /><br /> | |
| 计算结果<input type="text" id="get_result"> | |
| <!--计算结果<p id="result" ></p>--> | |
| <input type="button" value="+" id="add" onclick="check('+');"> | |
| <input type="button" value="-" id="jian" onclick="check('-');"> | |
| <input type="button" value="*" id="cheng" onclick="check('*');"> | |
| <input type="button" value="/" id="chu" onchange="check('/');"><br /><br /> | |
| <!--<input type="button" value="计算" onclick="jisuan();">--> | |
| <script> | |
| var c=0; | |
| function check(obj){ | |
| var a=parseInt(document.getElementById("first").value); | |
| var b=parseInt(document.getElementById("second").value); | |
| if(obj=='+'){ | |
| // alert("b"); | |
| c=parseInt(a)+parseInt(b); | |
| } | |
| if(obj=='-'){ | |
| c=parseInt(a)-parseInt(b); | |
| } | |
| if(obj=='*'){ | |
| c=parseInt(a)*parseInt(b); | |
| } | |
| if(obj=='/'){ | |
| c=parseInt(a)/parseInt(b); | |
| } | |
| var res = document.getElementById("get_result"); // 这里我刚开始的时候直接写的 .value ,但是不行,感觉是 a=b,b=c 但是a不等于c的原因 | |
| res.value = c; | |
| } | |
| </script> | |
|
</body>
本文转自
|
本文介绍了一个简单的网页版计算器实现方法,通过HTML与JavaScript结合,能够进行基本的加减乘除运算。用户输入两个数字并选择运算符后,点击相应按钮即可显示计算结果。

被折叠的 条评论
为什么被折叠?



