-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
91 lines (72 loc) · 2.45 KB
/
index.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
<html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BackgroundChange</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
#container {
text-align:center;
margin-top:80px;
}
body{
background-color: antiquewhite;
}
h2 {
margin-bottom: 35px;
}
h3 {
margin-top: 35px;
}
.button {
padding: 15px;
border: 1px solid #0000008a;
border-radius: 2px;
margin-right: 10px;
cursor: pointer;
}
.mainbox {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
row-gap: 15px;
}
</style>
</head>
<body>
<div id="container">
<h2>Background Color Switcher</h2>
<div class="mainbox">
<span class="button" id="red" style="background-color: red; color: #fff;">Red</span>
<span class="button" id="green" style="background-color: green; color: #fff;">Green</span>
<span class="button" id="white" style="background-color: white; color: #000;">White</span>
<span class="button" id="yellow" style="background-color: yellow; color: #000;">Yellow</span>
</div>
<h3>Click the Button and change the Background Color</h3>
</div>
<script>
let button = document.querySelectorAll('.button');
let body = document.querySelector('body');
button.forEach((item) => {
item.addEventListener('click', (e) => {
if (e.target.id === 'red'){
body.style.backgroundColor = e.target.id;
}
if (e.target.id === 'green'){
body.style.backgroundColor = e.target.id;
}
if (e.target.id === 'white'){
body.style.backgroundColor = e.target.id;
}
if (e.target.id === 'yellow'){
body.style.backgroundColor = e.target.id;
}
})
})
</script>
</body></html>