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

js简易计算器

程序员文章站 2022-03-06 13:10:47
...
  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. <input type="text" id="shu1" size="7">
  11. <select id="ys">
  12. <option>+</option>
  13. <option>-</option>
  14. <option>*</option>
  15. <option>/</option>
  16. <option>%</option>
  17. </select>
  18. <input type="text" id="shu2" size="7">
  19. <button type="button" id="js" value="btn">=</button>
  20. <p id="jg">结果:</p>
  21. <script>
  22. document.getElementById("js").onclick =function(){
  23. var shu1 = document.getElementById("shu1").value;
  24. var shu2 = document.getElementById("shu2").value;
  25. var ys = document.getElementById("ys").value;
  26. var zhjg =true;
  27. if(shu1==''||shu2==''){
  28. alert('输入框不能为空');
  29. return;
  30. }
  31. if(zhjg){
  32. switch(ys){
  33. case '+' :
  34. result = "结果:" + ((parseFloat(shu1)*100 + parseFloat(shu2)*100)/100);
  35. break;
  36. case '-' :
  37. result = "结果:" + (shu1 - shu2);
  38. break;
  39. case '*' :
  40. result = "结果:" + (shu1 * shu2);
  41. break;
  42. case '/' :
  43. if(shu2==0){
  44. result ="除数不能为0"
  45. }else{
  46. result = "结果:" + (shu1 / shu2);
  47. }
  48. break;
  49. case '%' :
  50. if(shu2==0){
  51. result ="除数不能为0"
  52. }else{
  53. result = "结果:" + (shu1 % shu2);
  54. }
  55. break;
  56. }
  57. }
  58. document.getElementById('jg').innerHTML=result;
  59. }
  60. </script>
  61. </body>
  62. </html>

js简易计算器
js简易计算器
js简易计算器