Blogger Information
Blog 70
fans 1
comment 0
visits 53054
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
简单计算器
葡萄枝子
Original
574 people have browsed it

简单计算器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <input type="text" name="num1" id="num1" value="">
  9. <select name="choice" id="choice">
  10. <option value="+">+</option>
  11. <option value="-">-</option>
  12. <option value="*">*</option>
  13. <option value="/">/</option>
  14. <option value="%">%</option>
  15. </select>
  16. <input type="text" name="num2" id="num2">
  17. <input type="button" id="calc" value="计算">
  18. <p id="res"></p>
  19. <script>
  20. document.getElementById('calc').onclick = function () {
  21. var num1 = document.getElementById('num1').value
  22. var num2 = document.getElementById('num2').value
  23. var choice = document.getElementById('choice').value
  24. var res = ''
  25. if (num1 == '' || isNaN(num1 * 1)) res += '第一个输入框必须为数字'
  26. if (num2 == '' || isNaN(num2 * 1)) res += '第二个输入框必须为数字'
  27. if ((choice == '/' || choice == '%') && num2 * 1 == 0) res += '第二个输入框不能为零'
  28. num1 = num1 * 1;
  29. num2 = num2 * 1;
  30. if (!res) {
  31. switch (choice) {
  32. case '+':
  33. res = num1 + num2
  34. break
  35. case '-':
  36. res = num1 - num2
  37. break
  38. case '*':
  39. res = num1 * num2
  40. break
  41. case '/':
  42. res = num1 / num2
  43. break
  44. default:
  45. res = num1 % num2
  46. }
  47. }
  48. document.getElementById('res').innerText = res
  49. }
  50. </script>
  51. </body>
  52. </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