Blogger Information
Blog 5
fans 1
comment 0
visits 4777
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS简单计算器
自带光芒
Original
542 people have browsed it





  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>简单计算器</title>
  7. </head>
  8. <body>
  9. <input type="text" name="" id="num1" value="" />
  10. <select name="" id="method">
  11. <option value="+" selected="selected">+</option>
  12. <option value="-">-</option>
  13. <option value="*">*</option>
  14. <option value="/">/</option>
  15. <option value="%">%</option>
  16. </select>
  17. <input type="text" name="" id="num2" value="" />
  18. <input type="button" name="" id="calc" value="计算" />
  19. <div class="" id="result">
  20. </div>
  21. <script type="text/javascript">
  22. document.getElementById('calc').onclick = function(){
  23. var num1 = document.getElementById('num1').value;
  24. var num2 = document.getElementById('num2').value;
  25. var method = document.getElementById('method').value;
  26. var res = 0;
  27. num1=parseFloat(num1);
  28. num2=parseFloat(num2);
  29. if(isNaN(num1)){
  30. document.getElementById('result').innerHTML = '第一个数不是数字';
  31. return;
  32. }
  33. if(isNaN(num2)){
  34. document.getElementById('result').innerHTML = '第二个数不是数字';
  35. return;
  36. }
  37. switch(method){
  38. case '+' :
  39. res = num1 + num2;
  40. document.getElementById('result').innerHTML = '运算结果:'+ num1 + '+' + num2 + '=' + res;
  41. break;
  42. case '-' :
  43. res = num1 - num2;
  44. document.getElementById('result').innerHTML = '运算结果:'+ num1 + '-' + num2 + '=' + res;
  45. break;
  46. case '*' :
  47. res = num1 * num2;
  48. document.getElementById('result').innerHTML = '运算结果:'+ num1 + '*' + num2 + '=' + res;
  49. break;
  50. case '/' :
  51. if(num2 == 0){
  52. document.getElementById('result').innerHTML = '除数不能为0';
  53. return;
  54. }
  55. res = num1 / num2;
  56. document.getElementById('result').innerHTML = '运算结果:'+ num1 + '/' + num2 + '=' + res;
  57. break;
  58. case '%' :
  59. if(num2 == 0){
  60. document.getElementById('result').innerHTML = '除数不能为0';
  61. return;
  62. }
  63. res = num1 % num2;
  64. document.getElementById('result').innerHTML = '运算结果:'+ num1 + '%' + num2 + '=' + res;
  65. break;
  66. }
  67. }
  68. </script>
  69. </body>
  70. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post