-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample14.html
493 lines (474 loc) · 15.8 KB
/
example14.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Document Object Model (DOM) Programming Tutorial</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 20px;
background-color: #f9f9f9;
color: #333;
}
h1,
h2 {
color: #444;
}
table {
border-collapse: collapse;
width: 100%;
margin: 10px 0;
}
th,
td {
padding: 10px;
text-align: left;
border: 1px solid #ddd;
}
th {
background-color: #4CAF50;
color: white;
}
.section {
margin-bottom: 40px;
padding: 20px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 10px;
}
.code-example {
background-color: #f8f9fa;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
margin: 10px 0;
}
.output {
background-color: #e9f7ef;
padding: 10px;
border: 1px solid #27ae60;
border-radius: 5px;
margin: 10px 0;
}
.interactive {
background-color: #e8f4f8;
padding: 15px;
border: 1px solid #3498db;
border-radius: 5px;
margin: 10px 0;
}
button {
background-color: #3498db;
color: white;
border: none;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #2980b9;
}
input[type="text"] {
padding: 5px;
border-radius: 3px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<h1>Interactive Document Object Model (DOM) Programming Tutorial</h1>
<div class="section">
<h2>Introduction to the DOM</h2>
<p>The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the structure of a document as a tree-like hierarchy of objects, allowing programs to dynamically access and manipulate the content, structure, and style of web documents.</p>
<h3>Key Concepts:</h3>
<ul>
<li>The DOM represents an HTML document as a tree structure</li>
<li>Each element, attribute, and piece of text in the HTML becomes a node in the tree</li>
<li>JavaScript can be used to manipulate the DOM, changing the document's structure, style, and content</li>
</ul>
</div>
<div class="section">
<h2>Accessing DOM Elements</h2>
<p>There are several ways to access elements in the DOM:</p>
<h3>1. getElementById()</h3>
<p>Retrieves an element by its ID attribute.</p>
<div class="code-example">
<pre>
<code>
// HTML: <div id="myDiv">Hello, DOM!</div>
const element = document.getElementById('myDiv');
console.log(element.textContent); // Outputs: Hello, DOM!
</code>
</pre>
</div>
<h3>2. getElementsByClassName()</h3>
<p>Returns a live HTMLCollection of elements with the given class name.</p>
<div class="code-example">
<pre>
<code>
// HTML: <p class="highlight">First</p><p class="highlight">Second</p>
const elements = document.getElementsByClassName('highlight');
console.log(elements.length); // Outputs: 2
</code>
</pre>
</div>
<h3>3. getElementsByTagName()</h3>
<p>Returns a live HTMLCollection of elements with the given tag name.</p>
<div class="code-example">
<pre>
<code>
const paragraphs = document.getElementsByTagName('p');
console.log(paragraphs.length); // Outputs: number of <p> elements
</code>
</pre>
</div>
<h3>4. querySelector()</h3>
<p>Returns the first element that matches a CSS selector.</p>
<div class="code-example">
<pre>
<code>
const element = document.querySelector('.highlight');
console.log(element.textContent); // Outputs: First
</code>
</pre>
</div>
<h3>5. querySelectorAll()</h3>
<p>Returns a static NodeList of all elements matching a CSS selector.</p>
<div class="code-example">
<pre>
<code>
const elements = document.querySelectorAll('.highlight');
elements.forEach(el => console.log(el.textContent));
</code>
</pre>
</div>
<h3>Interactive Example: Accessing DOM Elements</h3>
<div class="interactive">
<p id="demo-paragraph">This is a demo paragraph.</p>
<button onclick="changeText()">Change Text</button>
<script>
function changeText() {
const paragraph = document.getElementById('demo-paragraph');
paragraph.textContent = 'Hellooooooooooooo! I need a coffee!';
}
</script>
</div>
</div>
<div class="section">
<h2>Manipulating DOM Elements</h2>
<h3>Changing Content</h3>
<div class="code-example">
<pre>
<code>
const element = document.getElementById('myDiv');
element.textContent = 'New content';
element.innerHTML = '<strong>Bold content</strong>';
</code>
</pre>
</div>
<h3>Changing Attributes</h3>
<div class="code-example">
<pre>
<code>
const img = document.querySelector('img');
img.setAttribute('src', 'new-image.jpg');
img.alt = 'New description';
</code>
</pre>
</div>
<h3>Changing Styles</h3>
<div class="code-example">
<pre>
<code>
const element = document.getElementById('myDiv');
element.style.color = 'blue';
element.style.backgroundColor = 'yellow';
</code>
</pre>
</div>
<h3>Adding and Removing Classes</h3>
<div class="code-example">
<pre>
<code>
const element = document.getElementById('myDiv');
element.classList.add('highlight');
element.classList.remove('old-class');
element.classList.toggle('active');
</code>
</pre>
</div>
<h3>Interactive Example: Manipulating Styles</h3>
<div class="interactive">
<p id="style-demo">Click the button to change my style!</p>
<button onclick="changeStyle()">Change Style</button>
<script>
function changeStyle() {
const element = document.getElementById('style-demo');
element.style.color = 'blue';
element.style.fontSize = '20px';
element.style.fontWeight = 'bold';
}
</script>
</div>
</div>
<div class="section">
<h2>Creating and Removing Elements</h2>
<h3>Creating Elements</h3>
<div class="code-example">
<pre>
<code>
const newParagraph = document.createElement('p');
newParagraph.textContent = 'This is a new paragraph.';
document.body.appendChild(newParagraph);
</code>
</pre>
</div>
<h3>Removing Elements</h3>
<div class="code-example">
<pre>
<code>
const elementToRemove = document.getElementById('oldElement');
elementToRemove.parentNode.removeChild(elementToRemove);
// Or using the newer remove() method
elementToRemove.remove();
</code>
</pre>
</div>
<h3>Interactive Example: Creating and Removing Elements</h3>
<div class="interactive">
<div id="element-container"></div>
<button onclick="addElement()">Add Element</button>
<button onclick="removeElement()">Remove Element</button>
<script>
let elementCount = 0;
function addElement() {
elementCount++;
const container = document.getElementById('element-container');
const newElement = document.createElement('p');
newElement.textContent = `Element ${elementCount}`;
container.appendChild(newElement);
}
function removeElement() {
const container = document.getElementById('element-container');
if (container.lastChild) {
container.removeChild(container.lastChild);
elementCount--;
}
}
</script>
</div>
</div>
<div class="section">
<h2>Event Handling</h2>
<p>The DOM allows you to attach event listeners to elements, enabling interactive web pages.</p>
<h3>Adding Event Listeners</h3>
<div class="code-example">
<pre>
<code>
const button = document.getElementById('myButton');
button.addEventListener('click', function(event) {
console.log('Button clicked!');
event.preventDefault(); // Prevents default action
});
</code>
</pre>
</div>
<h3>Removing Event Listeners</h3>
<div class="code-example">
<pre>
<code>
function handleClick(event) {
console.log('Clicked!');
}
button.addEventListener('click', handleClick);
// Later, when you want to remove the listener:
button.removeEventListener('click', handleClick);
</code>
</pre>
</div>
<h3>Interactive Example: Event Handling</h3>
<div class="interactive">
<button id="event-demo">Click me!</button>
<p id="event-output"></p>
<script>
const button = document.getElementById('event-demo');
const output = document.getElementById('event-output');
let clickCount = 0;
button.addEventListener('click', function() {
clickCount++;
output.textContent = `Button clicked ${clickCount} time(s)!`;
});
</script>
</div>
</div>
<div class="section">
<h2>Traversing the DOM</h2>
<p>You can navigate through the DOM tree using various properties:</p>
<table>
<tr>
<th>Property</th>
<th>Description</th>
</tr>
<tr>
<td>parentNode</td>
<td>Returns the parent node of an element</td>
</tr>
<tr>
<td>childNodes</td>
<td>Returns a NodeList of child nodes</td>
</tr>
<tr>
<td>firstChild</td>
<td>Returns the first child node</td>
</tr>
<tr>
<td>lastChild</td>
<td>Returns the last child node</td>
</tr>
<tr>
<td>nextSibling</td>
<td>Returns the next node at the same tree level</td>
</tr>
<tr>
<td>previousSibling</td>
<td>Returns the previous node at the same tree level</td>
</tr>
</table>
<div class="code-example">
<pre>
<code>
const parentElement = document.getElementById('parent');
const childElements = parentElement.children;
const firstChild = parentElement.firstElementChild;
const lastChild = parentElement.lastElementChild;
</code>
</pre>
</div>
<h3>Interactive Example: DOM Traversal</h3>
<div class="interactive">
<div id="traversal-demo">
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</div>
<button onclick="highlightNext()">Highlight Next</button>
<button onclick="highlightPrevious()">Highlight Previous</button>
<script>
let currentIndex = -1;
const paragraphs = document.querySelectorAll('#traversal-demo p');
function highlightNext() {
if (currentIndex >= 0 && currentIndex < paragraphs.length) {
paragraphs[currentIndex].style.backgroundColor = '';
}
currentIndex = (currentIndex + 1) % paragraphs.length;
paragraphs[currentIndex].style.backgroundColor = 'yellow';
}
function highlightPrevious() {
if (currentIndex >= 0 && currentIndex < paragraphs.length) {
paragraphs[currentIndex].style.backgroundColor = '';
}
currentIndex = (currentIndex - 1 + paragraphs.length) % paragraphs.length;
paragraphs[currentIndex].style.backgroundColor = 'yellow';
}
</script>
</div>
</div>
<div class="section">
<h2>Working with Forms</h2>
<p>The DOM provides special properties and methods for working with forms:</p>
<div class="code-example">
<pre>
<code>
const form = document.getElementById('myForm');
const input = form.elements['username'];
form.addEventListener('submit', function(event) {
event.preventDefault();
console.log('Form submitted. Username:', input.value);
});
// Accessing form fields
const username = document.forms['myForm']['username'].value;
</code>
</pre>
</div>
<h3>Interactive Example: Form Handling</h3>
<div class="interactive">
<form id="demo-form">
<input type="text" id="name-input" placeholder="Enter your name">
<button type="submit">Submit</button>
</form>
<p id="form-output"></p>
<script>
const form = document.getElementById('demo-form');
const nameInput = document.getElementById('name-input');
const formOutput = document.getElementById('form-output');
form.addEventListener('submit', function(event) {
event.preventDefault();
formOutput.textContent = `Hello, ${nameInput.value}!`;
nameInput.value = '';
});
</script>
</div>
</div>
<div class="section">
<h2>DOM and AJAX</h2>
<p>The DOM can be updated dynamically using AJAX (Asynchronous JavaScript and XML) requests:</p>
<div class="code-example">
<pre>
<code>
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
const container = document.getElementById('dataContainer');
data.forEach(item => {
const element = document.createElement('div');
element.textContent = item.name;
container.appendChild(element);
});
})
.catch(error => console.error('Error:', error));
</code>
</pre>
</div>
</div>
<div class="section">
<h2>Best Practices and Performance Considerations</h2>
<ul>
<li>Minimize DOM manipulation to improve performance</li>
<li>Use document fragments for batch insertions</li>
<li>Be cautious with innerHTML due to security risks (prefer textContent when possible)</li>
<li>Use event delegation for handling multiple similar elements</li>
<li>Cache DOM references when accessing elements multiple times</li>
</ul>
</div>
<div class="section">
<h2>Browser Compatibility</h2>
<p>While most modern browsers support the standard DOM methods, always check browser compatibility for newer features. Use feature detection or polyfills when necessary to ensure cross-browser compatibility.</p>
</div>
<footer>
<p>This tutorial covers the fundamentals of DOM Programming. Practice and explore further to master these concepts!</p>
</footer>
<script>
// This script demonstrates some of the concepts covered in the tutorial
document.addEventListener('DOMContentLoaded', function() {
// Example: Changing content
const heading = document.querySelector('h1');
heading.style.color = '#4CAF50';
// Example: Event handling
const sections = document.querySelectorAll('.section');
sections.forEach(section => {
section.addEventListener('click', function() {
this.style.backgroundColor = '#f0f8ff';
});
});
// Example: Creating elements
const footer = document.querySelector('footer');
const currentDate = new Date().toLocaleDateString();
const dateElement = document.createElement('p');
dateElement.textContent = `Last updated: ${currentDate}`;
footer.appendChild(dateElement);
});
</script>
</body>
</html>