ASCII码 ASCII码

利用JavaScript制作99乘法表和简易计算器

发布于:2022-05-21 13:25:53  栏目:技术文档

九九乘法表效果图

代码

  1. //九九乘法表
  2. document.write('<table border="1" width="800" align="center">')
  3. for(var hang = 1; hang <= 9; hang++){
  4. document.write('<tr>')
  5. for (var td =1; td <= hang; td++){
  6. document.write('<td>' + td + 'X' + hang + '=' + ( td * hang ) + '</td>')
  7. }
  8. document.write('</tr>')
  9. }

简易计算器效果图

代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>简单计算器</title>
  6. <style>
  7. div{
  8. text-align: center;
  9. font-size: 24px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div>
  15. <input type="text" size="5" id="num1" value="">
  16. <select id="ysf">
  17. <option value="+">+</option>
  18. <option value="-">-</option>
  19. <option value="*">*</option>
  20. <option value="/">/</option>
  21. <option value="%">%</option>
  22. </select>
  23. <input type="text" size="5" id="num2" value="">
  24. <input type="button" value="计算" id="btn">
  25. </div>
  26. <div id="res"></div>
  27. <script>
  28. //获取按钮注册事件
  29. document.getElementById('btn').onclick=function(){
  30. var num1 = document.getElementById('num1').value;
  31. var num2 = document.getElementById('num2').value;
  32. var ysf = document.getElementById('ysf').value;
  33. var res = document.getElementById('res')
  34. //计算
  35. switch(ysf){
  36. case '+':
  37. res.innerHTML = num1 + '+' + num2 + '=' + (num1*1 + num2*1);
  38. break;
  39. case '-':
  40. res.innerHTML = num1 + '-' + num2 + '=' + (num1 - num2);
  41. break;
  42. case '*':
  43. res.innerHTML = num1 + 'x' + num2 + '=' + (num1 * num2);
  44. break;
  45. case '/':
  46. res.innerHTML = num1 + '÷' + num2 + '=' + (num1 / num2);
  47. break;
  48. case '%':
  49. res.innerHTML = num1 + '%' + num2 + '=' + (num1 % num2);
  50. break;
  51. }
  52. };
  53. </script>
  54. </body>
  55. </html>
相关推荐
阅读 +