欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单的计算器制作

程序员文章站 2022-03-06 13:14:08
...

简单的计算器制作 ,输入数字验证,暂时只能做到用数据转换,如果输入的是数字后加字母还是无法排除,
如:parseInt(123asd) = 123

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .div1 {
  8. width: 250px;
  9. height: 400px;
  10. background: #dbdbdb;
  11. border: 1px solid slategray;
  12. box-shadow: 0px 1px 3px 0px;
  13. border-radius: 5px 5px;
  14. margin: 30px 100px;
  15. }
  16. .d2 {
  17. text-align: center;
  18. }
  19. .mt-4 {
  20. margin-top: 10px;
  21. padding: 10px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <section class="div1 mt-4">
  27. <div class="d2 mt-4"><span>计算器</span></div>
  28. <input type="text" size="2" id="num1">
  29. <select name="select" id="select">
  30. <option value="+">+</option>
  31. <option value="-">-</option>
  32. <option value="*">*</option>
  33. <option value="/">/</option>
  34. <option value="%">%</option>
  35. </select>
  36. <input type="text" size="2" id="num2">
  37. <span>=</span>
  38. <span id="res"></span><br>
  39. <br>
  40. &nbsp;&nbsp; <input type="button" value="计算" id="btn"> &nbsp;&nbsp;<button id="reBtn">重置</button>
  41. <br>
  42. <br>
  43. <span id="outNum1"></span>
  44. <br>
  45. <span id="outNum2"></span>
  46. </section>
  47. </body>
  48. <script>
  49. document.getElementById('btn').onclick=function(){
  50. document.getElementById('outNum1').innerHTML="";
  51. document.getElementById('outNum2').innerHTML="";
  52. document.getElementById('res').innerHTML="";
  53. var num1 = parseInt(document.getElementById('num1').value);
  54. var num2 = parseInt(document.getElementById('num2').value);
  55. var maVa =document.getElementById('select').value;
  56. if (isNaN(num1)) {
  57. document.getElementById('outNum1').innerHTML ='第一个数据非数字,请重新输入!';
  58. return;
  59. }
  60. if (isNaN(num2)) {
  61. document.getElementById('outNum2').innerHTML ='第二个数据非数字,请重新输入!';
  62. return;
  63. }
  64. switch (maVa){
  65. case '+':
  66. document.getElementById('res').innerHTML=num1+num2;
  67. break
  68. case '-':
  69. document.getElementById('res').innerHTML =num1-num2;
  70. break;
  71. case '*':
  72. document.getElementById('res').innerHTML = num1 * num2;
  73. break
  74. case '/':
  75. if (num2==0){
  76. document.getElementById('outNum2').innerHTML ='第二个数据不能为0,请重新输入!';
  77. return;
  78. }
  79. document.getElementById('res').innerHTML = num1 / num2;
  80. break;
  81. case '%':
  82. if (num2==0){
  83. document.getElementById('outNum2').innerHTML ='第二个数据不能为0,请重新输入!';
  84. return;
  85. }
  86. document.getElementById('res').innerHTML =num1%num2;
  87. break;
  88. }
  89. };
  90. document.getElementById('reBtn').onclick = function (){
  91. document.getElementById('outNum1').innerHTML="";
  92. document.getElementById('outNum2').innerHTML="";
  93. document.getElementById('res').innerHTML="";
  94. document.getElementById('num1').value="";
  95. document.getElementById('num2').value="";
  96. }
  97. </script>
  98. </html>