本文實例為大家分享了JS實現蘋果計算器的具體代碼,供大家參考,具體內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
<!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < title >手機</ title > < style type = "text/css" > #phone{ position: relative; width: 380px; height: 700px; box-shadow: 1px 1px 10px #808080; margin: auto; border-radius: 30px; } .kj{ position: absolute; width: 8px; height: 60px; background-color: black; right: -8px ; top:100px; } .yl{ position: absolute; width: 8px; height: 80px; background-color: black; left: -8px; top: 85px; } .top{ width: 120px; height: 50px; /* box-shadow: 1px 1px 10px #808080; */ margin: auto; } .mkf{ float: left; width: 100px; height: 10px; border-radius: 5px; background-color: black; margin-right: 10px; margin-top: 20px; } .sxj{ float: left; width: 10px; height: 10px; border-radius: 5px; background-color: black; margin-top: 20px; } .screen{ width: 370px ; height: 600px; background-color: black; margin: auto; } .result-num{ height: 120px; /* padding-top: 30px; */ } .sp{ float: right; color: white; font-size: 50px; margin-top: 50px; } .num{ height: 480px; } .key{ width: 82.5px; height: 82.5px; border-radius: 50px; background-color: #808080; float: left; margin: 5px; text-align: center; line-height: 80px; font-size: 20px; color: white; } .first{ background-color: #B0B0B0; color: black; } .second{ background-color: orange; } .third{ width: 175px; } .home{ width: 45px; height: 45px; border-radius: 25px; background-color: #B0B0B0; margin: 3px auto 0px auto; } </ style > </ head > < body > < div id = "phone" > <!--開機鍵--> < div class = "kj" ></ div > <!--音量--> < div class = "yl" ></ div > <!-- 手機頂部 --> < div class = "top" > <!-- 麥克風 --> < div class = "mkf" ></ div > <!-- 攝像頭 --> < div class = "sxj" ></ div > </ div > <!-- 屏幕 --> < div class = "screen" > < div class = "result-num" > < span class = "sp" >0</ span > </ div > < div class = "num" > < div class = "key first" onclick = "clearCompute()" >AC</ div > < div class = "key first" onclick = "deleteLastNum()" >←</ div > < div class = "key first" >%</ div > < div class = "key second" onclick = "fun('/')" >/</ div > < div class = "key keynum" onclick = "fun(7)" >7</ div > < div class = "key keynum" onclick = "fun(8)" >8</ div > < div class = "key keynum" onclick = "fun(9)" >9</ div > < div class = "key second" onclick = "fun('*')" >*</ div > < div class = "key keynum" onclick = "fun(4)" >4</ div > < div class = "key keynum" onclick = "fun(5)" >5</ div > < div class = "key keynum" onclick = "fun(6)" >6</ div > < div class = "key second" onclick = "fun('-')" >-</ div > < div class = "key keynum" onclick = "fun(1)" >1</ div > < div class = "key keynum" onclick = "fun(2)" >2</ div > < div class = "key keynum" onclick = "fun(3)" >3</ div > < div class = "key second" onclick = "fun('+')" >+</ div > < div class = "key third keynum" onclick = "fun(0)" >0</ div > < div class = "key" onclick = "fun('.')" >.</ div > < div class = "key second" onclick = "compute()" >=</ div > </ div > </ div > <!-- home鍵 --> < div class = "home" > </ div > </ div > < script type = "text/javascript" > var span = document.querySelector(".sp"); /** * @param {Object} num * 點擊數字鍵盤 將數字替換到我們的span標簽內部 */ function fun(num){ var value = span.innerText; if(value == 0){ span.innerText = num; }else{ span.innerText = span.innerText.concat(num); } } /** * 計算結果的 */ function compute(){ var value = span.innerText; var result= eval(value); span.innerText = result; } /** * 清空計算區域 重置為0 */ function clearCompute(){ span.innerText="0"; } /** * 刪除計算區域的最后一個字符 */ function deleteLastNum(){ var value = span.innerText; console.log(typeof(value)) if(value == 0){ } else{ /** * value是一個字符串 * zs123 * 字節:文本在計算器底層存儲的時候都是字節存儲的 編碼集 將字符轉成特定的字節在計算機上存儲的 * 字符:人類能看懂的一個字母 或者一個中文漢字 * a b 中 國 aj * * 字符串就是一個一個字符組成的一個集合體 字符串String提供了很多常用的方法 以便我們對這個字符數組進行相關操作 */ if(value.length!=1){ span.innerText = value.substring(0,value.length-1) }else{ span.innerText="0"; } } } </ script > </ body > </ html > |
效果圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_45177027/article/details/115481766