-
Notifications
You must be signed in to change notification settings - Fork 165
/
calculator.html
92 lines (87 loc) · 1.83 KB
/
calculator.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>计算器</title>
<meta charset="utf-8">
<style>
body {
padding: 20px;
user-select: none;
}
#wrapper {
width: 320px;
}
#result {
height: 100px;
border: 1px solid #ddd;
padding: 20px;
box-sizing: border-box;
}
.row {
display: flex;
column-count: 3;
}
.row button {
padding: 20px;
flex: 1;
font-size: 16px;
border: 1px solid #ddd;
background: #eee;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="result"></div>
<div class="row">
<button>+</button>
<button>-</button>
<button id="ac">AC</button>
</div>
<div class="row">
<button>/</button>
<button>*</button>
<button id="getResult">=</button>
</div>
<div class="row">
<button>7</button>
<button>8</button>
<button>9</button>
</div>
<div class="row">
<button>4</button>
<button>5</button>
<button>6</button>
</div>
<div class="row">
<button>1</button>
<button>2</button>
<button>3</button>
</div>
<div class="row">
<button>%</button>
<button>0</button>
<button>.</button>
</div>
</div>
</body>
<script>
let buttonObjs = document.getElementsByTagName("button");
let resultObj = document.getElementById("result");
let acObj = document.getElementById("ac");
let getResultObj = document.getElementById("getResult");
for (const buttonObj of buttonObjs) {
if (!['AC', '='].includes(buttonObj.innerHTML)) {
buttonObj.addEventListener('click', () => {
resultObj.innerHTML += buttonObj.innerHTML;
})
}
}
acObj.addEventListener('click', () => {
resultObj.innerHTML = '';
})
getResultObj.addEventListener('click', () => {
resultObj.innerHTML = eval(resultObj.innerHTML);
})
</script>
</html>