用JS實現了一個簡單的計算器。


----恢復內容開始---

     上午第二節課拿着電腦去教室,又練了上周老師講的——用JS實現簡單的計算器功能。連續點擊數字按鈕實現字符串相連那部分還是有點不太明白,晚上要再練習一遍。

     還練習了網頁時鍾。

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>計算器</title>
 6     <style>
 7         div input{
 8             margin-top: 10px;
 9             height: 40px;
10             width: 40px;
11             border: none;
12             background-color: #999;
13         }
14         input{
15             border-radius: 8px;
16             
17         }
18     </style>
19 </head>
20 <body>
21     <input class="show" type="text" id="show">
22     <div>
23         <input type="button" value="1" id="number">
24         <input type="button" value="2" id="number">
25         <input type="button" value="3" id="number">
26         <input type="button" id="operate" value="+">
27     </div>
28     <div>
29         <input type="button" id="number" value="4">
30         <input type="button" id="number" value="5">
31         <input type="button" id="number" value="6">
32         <input type="button" id="operate" value="-">
33     </div>
34     <div>
35         <input type="button" id="number" value="7">
36         <input type="button" id="number" value="8">
37         <input type="button" id="number" value="9">
38         <input type="button" id="operate" value="*">
39     </div>
40     <div>
41         <input type="button" style="width: 130px;" id="number" value="0">
42         <input type="button" id="operate" value="/">
43     </div>
44     <div>
45         <input type="button" id="count" value="計算" style="width: 85px;">
46         <input type="button" id="clear" value="清除" style="width: 85px;">                
47     </div>
48     <script type="text/javascript" src="jquery-1.11.1.js"></script>
49     <script>
50         var shownum1 = "";
51         var shownum2 = "";
52         var ope = "";
53         $("input#number").click(function(){
54             if(ope == ""){
55                 $("#show").val(shownum1 + this.value);
56                 shownum1 = $("#show").val();
57             }else{
58                 $("#show").val(shownum2 + this.value);
59                 shownum2 = $("#show").val();
60             }
61         }).mousedown(function(){
62             $(this).css("background-color","#ccc");
63         }).mouseup(function(){
64             $(this).css("background-color","#999");
65         })
66         $("input#operate").click(function(){
67             ope = $(this).val();
68             // $("#show").val("");
69         }).mousedown(function(){
70             $(this).css("background-color","#ccc");
71         }).mouseup(function(){
72             $(this).css("background-color","#999");
73         })
74         $("#count").click(function(){
75             switch(ope){
76                 case "+" : $("#show").val(Number(shownum1) + Number(shownum2));break;
77                 case "-" : $("#show").val(Number(shownum1) - Number(shownum2));break;
78                 case "*" : $("#show").val(Number(shownum1) * Number(shownum2));break;
79                 case "/" : $("#show").val(Number(shownum1) / Number(shownum2));break;
80             }
81 
82         }).mousedown(function(){
83             $(this).css("background-color","#ccc");
84         }).mouseup(function(){
85             $(this).css("background-color","#999");
86         })
87         $("#clear").click(function(){
88             shownum1 = "";
89             shownum2 = "";
90             $("#show").val("");
91             ope = "";
92         }).mousedown(function(){
93             $(this).css("background-color","#ccc");
94         }).mouseup(function(){
95             $(this).css("background-color","#999");
96         })
97     </script>
98 </body>
99 </html>

 

---恢復內容結束--


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM