博客列表 >JS 简单计算器、九九乘法表实例演示

JS 简单计算器、九九乘法表实例演示

赵大叔
赵大叔原创
2022年05月19日 17:57:49673浏览

JS 简单计算器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>简单计算器</title>
  8. </head>
  9. <body>
  10. <div style="width: 200px;margin: auto; background-color:rgb(184, 249, 237);">
  11. <input type="text" size="5" id="num1" value="" />
  12. <select id="ysf">
  13. <option value="+">+</option>
  14. <option value="-">-</option>
  15. <option value="*">*</option>
  16. <option value="/">/</option>
  17. <option value="%">%</option>
  18. </select>
  19. <input type="text" size="5" id="num2" value=""/>
  20. <input type="button" value="计算" id="btn" />
  21. </div>
  22. <p id="res" style="width: 200px;margin: 10px auto; background-color:rgb(184, 249, 237);"></p>
  23. <script>
  24. //获取按钮注册事件
  25. document.getElementById('btn').onclick=function(){
  26. var num1 = document.getElementById('num1').value;
  27. var num2 = document.getElementById('num2').value;
  28. var ysf = document.getElementById('ysf').value;
  29. // 简单判断输入的内容只能是数字
  30. if (num1 == NaN || num2 == NaN){
  31. window.alert('请输入数字进行计算');
  32. }else{
  33. // document.getElementById('res').innerHTML=num1+"---"+num2+"---"+ysf;
  34. var res = document.getElementById('res');
  35. if(ysf == '+'){
  36. res.innerHTML = num1 + ysf + num2 + '=' + (parseInt(num1) + parseInt(num2));
  37. };
  38. if(ysf == '-'){
  39. res.innerHTML = num1 + ysf + num2 + '=' + (num1 - num2);
  40. };
  41. if(ysf == '*'){
  42. res.innerHTML = num1 + ysf + num2 + '=' + (num1 * num2);
  43. };
  44. if(ysf == '/'){
  45. // 判断被除数不能为0
  46. if(num2 == 0){
  47. window.alert('被除数不能为0');
  48. }else{
  49. res.innerHTML = num1 + ysf + num2 + '=' + (num1 / num2);
  50. }
  51. };
  52. }
  53. };
  54. </script>
  55. </body>
  56. </html>

九九乘法表

上图

js代码

  1. <script>
  2. document.write('<table border="1" width="800" align="center">')
  3. for(var row = 9; row > 0; row--){
  4. document.write('<tr>')
  5. for(var col = 1; col <= row; col++){
  6. document.write('<td>' + col + '*' + row + '=' + col * row + '</td>')
  7. // console.log(col + '*' + row + '=' + col * row)
  8. }
  9. document.write('</tr>')
  10. }
  11. document.write('</table>')
  12. </script>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议