1<!DOCTYPE html>
2<html lang="ko">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>DOM ์กฐ์๊ณผ ์ด๋ฒคํธ ์์ </title>
7 <style>
8 body {
9 font-family: 'Segoe UI', Tahoma, sans-serif;
10 max-width: 900px;
11 margin: 0 auto;
12 padding: 20px;
13 background: #f5f5f5;
14 }
15 h1 { text-align: center; color: #333; }
16 h2 { color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 10px; }
17 .section {
18 background: white;
19 padding: 20px;
20 margin: 20px 0;
21 border-radius: 8px;
22 box-shadow: 0 2px 5px rgba(0,0,0,0.1);
23 }
24 button {
25 background: #3498db;
26 color: white;
27 border: none;
28 padding: 10px 20px;
29 border-radius: 4px;
30 cursor: pointer;
31 margin: 5px;
32 }
33 button:hover { background: #2980b9; }
34 .demo-box {
35 padding: 20px;
36 margin: 10px 0;
37 border: 2px dashed #ddd;
38 border-radius: 8px;
39 min-height: 100px;
40 }
41 .highlight { background: #f1c40f; }
42 .output {
43 background: #2c3e50;
44 color: #2ecc71;
45 padding: 15px;
46 border-radius: 4px;
47 font-family: monospace;
48 margin: 10px 0;
49 }
50 input, select {
51 padding: 10px;
52 margin: 5px;
53 border: 1px solid #ddd;
54 border-radius: 4px;
55 }
56 .event-log {
57 max-height: 200px;
58 overflow-y: auto;
59 font-size: 0.9em;
60 }
61 .card {
62 display: inline-block;
63 width: 100px;
64 height: 100px;
65 margin: 10px;
66 background: #3498db;
67 color: white;
68 display: flex;
69 align-items: center;
70 justify-content: center;
71 border-radius: 8px;
72 cursor: pointer;
73 transition: transform 0.3s, background 0.3s;
74 }
75 .card:hover { transform: scale(1.1); }
76 .card.selected { background: #e74c3c; }
77 #dragContainer {
78 display: flex;
79 gap: 20px;
80 min-height: 150px;
81 }
82 .drop-zone {
83 flex: 1;
84 min-height: 120px;
85 border: 3px dashed #bdc3c7;
86 border-radius: 8px;
87 padding: 10px;
88 display: flex;
89 flex-wrap: wrap;
90 align-content: flex-start;
91 }
92 .drop-zone.drag-over {
93 border-color: #3498db;
94 background: #ecf0f1;
95 }
96 .draggable {
97 width: 60px;
98 height: 60px;
99 background: #9b59b6;
100 color: white;
101 display: flex;
102 align-items: center;
103 justify-content: center;
104 border-radius: 8px;
105 cursor: move;
106 margin: 5px;
107 }
108 </style>
109</head>
110<body>
111 <h1>DOM ์กฐ์๊ณผ ์ด๋ฒคํธ ์์ </h1>
112
113 <!-- 1. ์์ ์ ํ -->
114 <div class="section">
115 <h2>1. ์์ ์ ํ</h2>
116 <div id="selectDemo">
117 <p id="uniqueId">ID๋ก ์ ํ๋ ์์</p>
118 <p class="myClass">ํด๋์ค๋ก ์ ํ 1</p>
119 <p class="myClass">ํด๋์ค๋ก ์ ํ 2</p>
120 <div class="container">
121 <span data-info="test">data ์์ฑ์ผ๋ก ์ ํ</span>
122 </div>
123 </div>
124 <button onclick="demoSelection()">์ ํ ํ
์คํธ</button>
125 <div class="output" id="selectOutput"></div>
126 </div>
127
128 <!-- 2. ์์ ์์ฑ ๋ฐ ์ถ๊ฐ -->
129 <div class="section">
130 <h2>2. ์์ ์์ฑ ๋ฐ ์ถ๊ฐ</h2>
131 <input type="text" id="newItemInput" placeholder="์ ํญ๋ชฉ ์
๋ ฅ">
132 <button onclick="addItem()">ํญ๋ชฉ ์ถ๊ฐ</button>
133 <button onclick="addItemBefore()">๋งจ ์์ ์ถ๊ฐ</button>
134 <button onclick="clearItems()">์ ์ฒด ์ญ์ </button>
135 <ul id="itemList" class="demo-box">
136 <li>๊ธฐ์กด ํญ๋ชฉ 1</li>
137 <li>๊ธฐ์กด ํญ๋ชฉ 2</li>
138 </ul>
139 </div>
140
141 <!-- 3. ์คํ์ผ ๋ฐ ํด๋์ค ์กฐ์ -->
142 <div class="section">
143 <h2>3. ์คํ์ผ ๋ฐ ํด๋์ค ์กฐ์</h2>
144 <div id="styleBox" class="demo-box" style="transition: all 0.3s;">
145 ์คํ์ผ ์กฐ์ ๋์
146 </div>
147 <button onclick="changeColor()">์์ ๋ณ๊ฒฝ</button>
148 <button onclick="toggleHighlight()">ํ์ด๋ผ์ดํธ ํ ๊ธ</button>
149 <button onclick="addBorder()">ํ
๋๋ฆฌ ์ถ๊ฐ</button>
150 <button onclick="resetStyle()">์ด๊ธฐํ</button>
151 </div>
152
153 <!-- 4. ์์ฑ ์กฐ์ -->
154 <div class="section">
155 <h2>4. ์์ฑ ์กฐ์</h2>
156 <img id="demoImage" src="https://via.placeholder.com/200x100?text=Image+1" alt="๋ฐ๋ชจ ์ด๋ฏธ์ง" style="border-radius: 8px;">
157 <br><br>
158 <button onclick="changeImage(1)">์ด๋ฏธ์ง 1</button>
159 <button onclick="changeImage(2)">์ด๋ฏธ์ง 2</button>
160 <button onclick="changeImage(3)">์ด๋ฏธ์ง 3</button>
161 <br>
162 <input type="text" id="altInput" placeholder="alt ํ
์คํธ ์
๋ ฅ">
163 <button onclick="changeAlt()">alt ๋ณ๊ฒฝ</button>
164 <p>ํ์ฌ alt: <span id="currentAlt"></span></p>
165 </div>
166
167 <!-- 5. ์ด๋ฒคํธ ๋ฆฌ์ค๋ -->
168 <div class="section">
169 <h2>5. ์ด๋ฒคํธ ๋ฆฌ์ค๋</h2>
170 <div id="eventBox" class="demo-box" style="cursor: pointer; user-select: none;">
171 ์ด ์์ญ์์ ๋ค์ํ ์ด๋ฒคํธ๋ฅผ ํ
์คํธํ์ธ์.
172 <br>ํด๋ฆญ, ๋๋ธํด๋ฆญ, ๋ง์ฐ์ค ์ด๋, ํค๋ณด๋ ์
๋ ฅ ๋ฑ
173 </div>
174 <div class="output event-log" id="eventLog"></div>
175 <button onclick="clearEventLog()">๋ก๊ทธ ์ด๊ธฐํ</button>
176 </div>
177
178 <!-- 6. ์ด๋ฒคํธ ์์ -->
179 <div class="section">
180 <h2>6. ์ด๋ฒคํธ ์์ (Event Delegation)</h2>
181 <p>๊ฐ๋ณ ์์์ ์ด๋ฒคํธ๋ฅผ ๋ฑ๋กํ์ง ์๊ณ , ๋ถ๋ชจ์์ ์ฒ๋ฆฌํฉ๋๋ค.</p>
182 <div id="cardContainer" class="demo-box">
183 <div class="card" data-id="1">1</div>
184 <div class="card" data-id="2">2</div>
185 <div class="card" data-id="3">3</div>
186 <div class="card" data-id="4">4</div>
187 <div class="card" data-id="5">5</div>
188 </div>
189 <p>์ ํ๋ ์นด๋: <span id="selectedCard">์์</span></p>
190 <button onclick="addCard()">์นด๋ ์ถ๊ฐ</button>
191 </div>
192
193 <!-- 7. ํผ ์ด๋ฒคํธ -->
194 <div class="section">
195 <h2>7. ํผ ์ด๋ฒคํธ</h2>
196 <form id="demoForm">
197 <input type="text" id="nameInput" placeholder="์ด๋ฆ" required>
198 <input type="email" id="emailInput" placeholder="์ด๋ฉ์ผ" required>
199 <select id="countrySelect">
200 <option value="">๊ตญ๊ฐ ์ ํ</option>
201 <option value="kr">ํ๊ตญ</option>
202 <option value="us">๋ฏธ๊ตญ</option>
203 <option value="jp">์ผ๋ณธ</option>
204 </select>
205 <button type="submit">์ ์ถ</button>
206 </form>
207 <div class="output" id="formOutput">ํผ ๋ฐ์ดํฐ๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค.</div>
208 </div>
209
210 <!-- 8. ํค๋ณด๋ ์ด๋ฒคํธ -->
211 <div class="section">
212 <h2>8. ํค๋ณด๋ ์ด๋ฒคํธ</h2>
213 <input type="text" id="keyInput" placeholder="์ฌ๊ธฐ์ ํ์ดํํ์ธ์" style="width: 300px;">
214 <div class="output" id="keyOutput">ํค ์ ๋ณด๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค.</div>
215 </div>
216
217 <!-- 9. ๋๋๊ทธ ์ค ๋๋กญ -->
218 <div class="section">
219 <h2>9. ๋๋๊ทธ ์ค ๋๋กญ</h2>
220 <div id="dragContainer">
221 <div class="drop-zone" id="zone1">
222 <div class="draggable" draggable="true" id="drag1">1</div>
223 <div class="draggable" draggable="true" id="drag2">2</div>
224 <div class="draggable" draggable="true" id="drag3">3</div>
225 </div>
226 <div class="drop-zone" id="zone2"></div>
227 </div>
228 </div>
229
230 <!-- 10. ์คํฌ๋กค ์ด๋ฒคํธ -->
231 <div class="section">
232 <h2>10. ์คํฌ๋กค ์ด๋ฒคํธ</h2>
233 <div id="scrollBox" style="height: 200px; overflow-y: scroll; border: 1px solid #ddd; padding: 10px;">
234 <div style="height: 600px;">
235 <p>์คํฌ๋กคํ์ธ์!</p>
236 <p style="position: sticky; top: 0; background: #3498db; color: white; padding: 10px;">
237 ์คํฌ๋กค ์์น: <span id="scrollPosition">0</span>px
238 </p>
239 <p>Lorem ipsum dolor sit amet...</p>
240 <p>๋ ๋ง์ ์ฝํ
์ธ ...</p>
241 <p>๊ณ์ ์คํฌ๋กค...</p>
242 <p>๊ฑฐ์ ๋...</p>
243 <p style="margin-top: 400px;">๋!</p>
244 </div>
245 </div>
246 </div>
247
248 <script src="script.js"></script>
249</body>
250</html>