Blogger Information
Blog 16
fans 0
comment 1
visits 8595
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js简易计算器
浅若夏沫
Original
568 people have browsed it
  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>



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