你的位置:首页 > Java教程

[Java教程]javascript简单计算器实践


  参考部分资料,编写一个简单的计算器案例,虽然完成了正常需求,但是也有不满之处,待后续实力提升后再来补充,先把不足之处列出:

  1:本来打算只要打开页面,计算器的输入框会显示一个默认为0的状态,但是在输入框加入默认显示为0的时候,选择数据输入时,该0会显示输入数字的前面,例如”0123“,由于能力有限,待后续实力提升再来补充完善!

  2:目前只能实现鼠标控制选择按钮,待完善键盘录入功能。

  html部分:

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4   <meta charset="utf-8"> 5   <title>计算器</title> 6 </head> 7 <body onload="onLoad()"> 8 <div id="calculator"> 9   <div class="LOGO">10     <span class="name">简单的计算器</span>11     <span class="verson">@liumobai v1.0</span>12   </div>13   <div id="shuRu">14     <!--screen输入栏-->15     <div class="screen">16       <input type="text" id="screenName" name="screenName" class="screen" value="" onfocus="jsq(this)">17     </div>18   </div>19   <div id="keys">20     <!-- operators and other keys -->21     <!--第一排-->22     <input type="button" id="7" onclick="jsq(this.id)" value="7" class="buttons">23     <input type="button" id="8" onclick="jsq(this.id)" value="8" class="buttons">24     <input type="button" id="9" onclick="jsq(this.id)" value="9" class="buttons">25     <input type="button" id="Back" onclick="tuiGe()" value="Back" class="buttons">26     <input type="button" id="C" onclick="clearNum()" value="C" class="buttons" style="margin-right:0px">27     <!--第二排-->28     <input type="button" id="4" onclick="jsq(this.id)" value="4" class="buttons">29     <input type="button" id="5" onclick="jsq(this.id)" value="5" class="buttons">30     <input type="button" id="6" onclick="jsq(this.id)" value="6" class="buttons">31     <input type="button" id="*" onclick="jsq(this.id)" value="*" class="buttons">32     <input type="button" id="/" onclick="jsq(this.id)" value="/" class="buttons" style="margin-right:0px">33     <!--第三排-->34     <input type="button" id="1" onclick="jsq(this.id)" value="1" class="buttons">35     <input type="button" id="2" onclick="jsq(this.id)" value="2" class="buttons">36     <input type="button" id="3" onclick="jsq(this.id)" value="3" class="buttons">37     <input type="button" id="+" onclick="jsq(this.id)" value="+" class="buttons">38     <input type="button" id="-" onclick="jsq(this.id)" value="-" class="buttons" style="margin-right:0px">39     <!--第四排-->40     <input type="button" id="0" onclick="jsq(this.id)" value="0" class="buttons">41     <input type="button" id="00" onclick="jsq(this.id)" value="00" class="buttons">42     <input type="button" id="." onclick="jsq(this.id)" value="." class="buttons">43     <input type="button" id="%" onclick="jsq(this.id)" value="%" class="buttons">44     <input type="button" id="eva" onclick="eva()" value="=" class="buttons" style="margin-right:0px">45   </div>46   <div class="footer">47     <span class="aside">欢迎使用JavaScript计算器</span>48       <span class="link">49         <a href="#" title="声明" target="_blank">反馈</a>50       </span>51   </div>52 </div>53 </body>54 </html>

  JS部分:

 1   <script> 2     var num = 0; // 定义第一个输入的数据 3     function jsq(num) { 4       //获取当前输入 5       document.getElementById('screenName').value += document.getElementById(num).value; 6     } 7     function eva() { 8       //计算输入结果 9       document.getElementById("screenName").value = eval(document.getElementById("screenName").value);10     }11     function clearNum() {12       //清013       document.getElementById("screenName").value = null;14       document.getElementById("screenName").focus();15     }16     function tuiGe() {17       //退格18       var arr = document.getElementById("screenName");19       arr.value = arr.value.substring(0, arr.value.length - 1);20     }21     function onLoad(){22       //加载完毕后光标自动对应到输入框23       document.getElementById("screenName").focus();24     }25   </script>

  CSS部分: 

 1 /*Basic reset*/ 2 *{ 3   margin:0; 4   padding:0; 5   box-sizing: border-box; 6   font: 14px Arial,sans-serif; 7 } 8 html{ 9   height:100%;10   background-color:lightslategrey;11 }12 13 #calculator{14   margin: 15px auto;15   width:330px;16   height:400px;17   border: 1px solid lightgray;18   background-color:darkgrey;19   padding:15px;20 }21 22 /*LOGO*/23 .LOGO{24   height:20px;25 26 }27 .LOGO .name{28   float:left;29   line-height:30px;30 }31 .LOGO .verson{32   float:right;33   line-height:30px;34 }35 /*screen*/36 #shuRu{37   margin-top:15px;38 }39 .screen{40   margin-top:5px;41   width:300px;42   height:40px;43   text-align: right;44   padding-right:10px;45   font-size:20px;46 }47 #keys{48   border:1px solid lightgray;49   height:223px;50   margin-top:25px;51   padding:8px;52 }53 #keys .last{54   margin-right:0px;55 }56 .footer{57   margin-top:20px;58   height:20px;59 }60 .footer .link{61   float:right;62 }63 64 #keys .buttons{65   float:left;66   width: 42px;67   height: 36px;68   text-align:center;69   background-color:lightgray;70   margin: 0 17px 20px 0;71 }