Blogger Information
Blog 7
fans 0
comment 0
visits 3286
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript格式的计算器以及乘法表
凝思
Original
426 people have browsed it

简版计算器

JavaScript格式的简版计算器

  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: 30px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div>
  15. <input type="number" size="5" id="num1" value="" />
  16. <select id="ysf">
  17. <option value="+">+</option>
  18. <option value="-">-</option>
  19. <option value="*">x</option>
  20. <option value="/">÷</option>
  21. <option value="%">%</option>
  22. </select>
  23. <input type="number" size="5" id="num2" value=""/>
  24. <input type="button" value="计算" id="btn" />
  25. </div>
  26. <div id="res"></div>
  27. <script>
  28. document.getElementById('btn').onclick=function(){
  29. var num1 = document.getElementById('num1').value;
  30. var num2 = document.getElementById('num2').value;
  31. var ysf = document.getElementById('ysf').value;
  32. var res = document.getElementById('res')
  33. //判断是否填写了数字再进行计算
  34. if (num1 == "" || num2 == "") {
  35. alert('请完整输入数字进行计算');
  36. }else{
  37. switch(ysf){
  38. case '+':
  39. res.innerHTML = num1 + '+' + num2 + "=" + (num1 * 1 + num2 * 1);
  40. break;
  41. case '-':
  42. res.innerHTML = num1 + '-' + num2 + '=' + (num1 - num2);
  43. break;
  44. case '*':
  45. res.innerHTML = num1 + 'x' + num2 + '=' + (num1 * num2);
  46. break;
  47. case '/':
  48. res.innerHTML = num1 + '÷' + num2 + '=' + (num1 / num2);
  49. break;
  50. case '%':
  51. res.innerHTML = num1 + '%' + num2 + '=' + (num1 % num2);
  52. break;
  53. }
  54. }
  55. //判断被除数是否为零
  56. if(ysf == '/') {
  57. if (num2 == 0) {
  58. alert('被除数不能为0');
  59. }
  60. }
  61. }
  62. </script>
  63. </body>
  64. </html>

简版计算器的效果图

简版计算器的效果图

小知识点

涉及到JavaScript中的 if 判断
document.getElementById 查找页面代码中的ID


九九乘法表

JavaScript格式递减排序九九乘法表

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>九九乘法表</title>
  6. <style>
  7. table{
  8. width: 800px;
  9. alignment: center;
  10. margin: auto;
  11. }
  12. td{
  13. text-align: center;
  14. border: 1px solid black;
  15. }
  16. td:hover{
  17. color: white;
  18. background: #747474;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <script>
  24. document.write('<table>')
  25. //递增排列方式: for (var hang = 1; hang <= 9; hang ++){
  26. for (var hang = 9; hang > 0; hang --) {
  27. document.write('<tr>')
  28. for (var td = 1; td <= hang; td++) {
  29. document.write('<td>' + td + 'X' + hang + '=' + (td * hang) + '</td>')
  30. }
  31. document.write('<tr/>')
  32. }
  33. document.write('</table>')
  34. </script>
  35. </body>
  36. </html>

九九乘法表的效果图

九九乘法表的效果图

小知识点

涉及到JavaScript中的 for 循环
++ 递增、- - 递减

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