-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00. DOM ? .js
More file actions
237 lines (188 loc) · 7.46 KB
/
00. DOM ? .js
File metadata and controls
237 lines (188 loc) · 7.46 KB
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
// DOM (Document Object Model) ?
/*
What is DOM?
The DOM is a programming interface for web documents that represents the structure of a document as a tree of objects. Each node corresponds to a part of the document (elements, attributes, text), allowing JavaScript to dynamically interact with and modify web page content, structure, and styling.
*/
// - The DOM is a programming interface for web documents.
// - It represents the structure of a document as a tree of objects.
// - Each node in the tree corresponds to a part of the document (e.g., elements, attributes, text).
// - The DOM allows programming languages (like JavaScript) to interact with the document content and structure.
// - You can use the DOM to dynamically change the content, structure, and style of a web page.
/*
Subtopics :
- selects, modify, create, delete elements
- Element selection (querySelector, getElementById, getElementsByClassName, getElementsByTagName)
- Content manipulation (innerHTML, textContent, innerText)
- Class management (classList.add, classList.remove, classList.toggle, classList.contains)
- Style manipulation (element.style, getComputedStyle)
- Event handling (addEventListener, removeEventListener)
- Form handling (form events, input.value, checkbox.checked, select.selectedIndex)
- Local storage and session storage (window.localStorage, window.sessionStorage)
- DOM tree traversal (parentNode, childNodes, siblings, children)
- Element creation and deletion (createElement, remove, appendChild, insertBefore)
- Event delegation (handling events on parent elements)
- Data attributes (element.dataset)
- Collections handling (HTMLCollection, NodeList, Array.from)
- Preventing default behavior (event.preventDefault)
- Dynamic class toggling (classList.toggle)
- Basic DOM security (avoiding XSS, sanitizing input)
- MutationObserver (watching for DOM changes)
- Performance optimization (minimizing reflows/repaints)
- Accessibility features (ARIA attributes, tab order, focus management)
- Responsive design with DOM (media queries, dynamic resizing)
- Working with SVG and Canvas elements via DOM
- Using templates (template tag, cloning nodes)
- Shadow DOM (encapsulation for web components)
- Debugging DOM issues (browser dev tools, console inspection)
*/
// Element Selection & Query Methods
// --------------------------------------------------------
// Basic selectors (must know)
document.getElementById('myId')
document.getElementsByClassName('myClass')
document.getElementsByTagName('div')
// Modern query selectors (preferred)
document.querySelector('.class')
document.querySelectorAll('div.class')
document.querySelector('#id')
// Element Modification
// Content manipulation
element.innerHTML = '<p>HTML content</p>'
element.textContent = 'Plain text only'
element.innerText = 'Visible text only'
// Attribute manipulation
element.getAttribute('data-id')
element.setAttribute('class', 'new-class')
element.removeAttribute('disabled')
// Style & Class Management
// --------------------------------------------------------
// Style manipulation
element.style.color = 'red'
element.style.display = 'none'
// Class management (modern approach)
element.classList.add('active')
element.classList.remove('hidden')
element.classList.toggle('highlight')
element.classList.contains('selected')
// DOM Tree Traversal
// --------------------------------------------------------`
// Navigation
element.parentNode
element.childNodes
element.firstChild / element.lastChild
element.nextSibling / element.previousSibling
// Modern alternatives (elements only)
element.parentElement
element.children
element.firstElementChild / element.lastElementChild
element.nextElementSibling / element.previousElementSibling
// Event Handling
// --------------------------------------------------------
// Event listeners (preferred method)
element.addEventListener('click', handleClick)
element.addEventListener('submit', handleSubmit)
element.addEventListener('keydown', handleKeyPress)
// Event delegation (performance optimization)
parentElement.addEventListener('click', function (e) {
if (e.target.matches('.child-button')) {
// Handle click on dynamically added buttons
}
})
// Prevent default behavior
event.preventDefault()
event.stopPropagation()
// Element Creation & Manipulation
// --------------------------------------------------------
// Creating elements
const newDiv = document.createElement('div')
const newText = document.createTextNode('Hello')
// Adding to DOM
parent.appendChild(newDiv)
parent.insertBefore(newDiv, existingChild)
parent.removeChild(childElement)
// Modern alternatives
parent.append(newDiv) // More flexible
parent.remove() // Remove element itself
// Browser Storage
// --------------------------------------------------------
// Local Storage (persists after browser close)
localStorage.setItem('key', 'value')
localStorage.getItem('key')
localStorage.removeItem('key')
// Session Storage (clears when tab closes)
sessionStorage.setItem('key', 'value')
sessionStorage.getItem('key')
// Form & Input Handling
// --------------------------------------------------------
// Form properties
input.value
checkbox.checked
select.selectedIndex
input.disabled
// Form events
form.addEventListener('submit', handleSubmit)
input.addEventListener('input', handleInput) // Real-time
input.addEventListener('change', handleChange) // On blur
// Data Attributes
// --------------------------------------------------------
// HTML: <div data-user-id="123" data-role="admin">
element.dataset.userId // "123"
element.dataset.role // "admin"
element.dataset.newProp = "value" // Sets data-new-prop
// Collections & Conversion
// --------------------------------------------------------
// HTMLCollection vs NodeList
const htmlCollection = document.getElementsByClassName('item') // Live
const nodeList = document.querySelectorAll('.item') // Static
// Convert to Array for easier manipulation
const itemsArray = Array.from(htmlCollection)
const itemsSpread = [...nodeList]
// Security Considerations
// --------------------------------------------------------
// Avoid XSS with user input
// BAD: element.innerHTML = userInput
// GOOD: element.textContent = userInput
// Or sanitize HTML properly
function sanitizeHTML(str) {
const temp = document.createElement('div')
temp.textContent = str
return temp.innerHTML
}
//Performance Tips
// --------------------------------------------------------
// Minimize reflows/repaints
// BAD: Multiple style changes
element.style.width = '100px'
element.style.height = '100px'
element.style.background = 'red'
// GOOD: Batch changes
element.style.cssText = 'width: 100px; height: 100px; background: red;'
// Or use classes
element.className = 'optimized-styles'
// MutationObserver
const observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
console.log('DOM changed:', mutation)
})
})
observer.observe(targetElement, {
childList: true,
attributes: true,
subtree: true
})
// Template & Shadow DOM
// Using templates
const template = document.querySelector('#my-template')
const clone = template.content.cloneNode(true)
// Shadow DOM (Web Components)
const shadowRoot = element.attachShadow({ mode: 'open' })
shadowRoot.innerHTML = '<p>Encapsulated content</p>'
// Accessibility Basics
// ARIA attributes
element.setAttribute('aria-label', 'Close button')
element.setAttribute('aria-expanded', 'false')
element.setAttribute('role', 'button')
// Focus management
element.tabIndex = 0 // Make focusable
element.focus()
element.blur()