250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- tailwindcss
- hardhat
- nextJS
- built in object
- Interface
- concept
- Redux
- error
- HTML
- CLASS
- useState
- API
- CSS
- JavaScript
- express.js
- node.js
- bitcoin
- evm
- 삶
- blockchain
- graphQL
- solidity
- web
- REACT
- Ethereum
- typeScript
- middleware
- Props
- 기준
- SSR
Archives
- Today
- Total
ReasonJun
javascript : event (delegation, event bubbling) 본문
728x90
delegation
// event delegation
// If you need to handle events on multiple elements in a similar pattern,
// You can use the event delegation pattern, controlled by a single ancestor element.
const parentEl = document.querySelector('.parent');
const childEls = document.querySelectorAll('.child');
// Event registration for all targets!
childEls.forEach((el) => {
el.addEventListener('click', (event) => {
console.log(event.target.textContent);
});
});
// Delegating events to ancestor elements
// It can have the same effect as above.
parentEl.addEventListener('click', (event) => {
const childEl = event.target.closest('.child');
if (childEl) {
console.log(childEl.textContent);
}
});
event bubbling => capture / stopPropagation
// Prevent event propagation (bubbles)
parentEl.addEventListener('click', (event) => {
event.stopPropagation();
});
// Fixed order of events (allows higher level events to fire first, starting with upper level options with 'capture' set)
document.window.addEventListener('click', (event) => {
console.log('window');
});
document.body.addEventListener(
'click',
(event) => {
console.log('body');
},
{ capture: true }
); // body works first.
document.parentEl.addEventListener(
'click',
(event) => {
console.log('parent');
},
{ capture: true }
); // When the body is clicked, the parent is executed after the body, and the child window is executed.
document.childEl.addEventListener(
'click',
(event) => {
console.log('child');
},
{ capture: true }
);
// What happen in this code if i click child?
document.window.addEventListener(
'click',
(event) => {
console.log('window');
},
{ capture: true }
);
document.body.addEventListener(
'click',
(event) => {
console.log('body');
event.stopPropagation();
},
{ capture: true }
); // window => body // end
document.parentEl.addEventListener(
'click',
(event) => {
console.log('parent');
},
{ capture: true }
);
document.childEl.addEventListener(
'click',
(event) => {
console.log('child');
},
{ capture: true }
);
// Other uses of capture
parentEl.addEventListener('click', handler, {
capture: true,
});
parentEl.removeEventListener('click', handler); // Remove does not work due to the capture in the code above.
// to make it work
parentEl.removeEventListener('click', handler, {
capture: true,
});
728x90
'Frontend > Javasciprt' 카테고리의 다른 글
javascript : event (handler once, passive, keyboard) (0) | 2023.06.09 |
---|---|
javascript : event (event obj, focus form event) (0) | 2023.06.09 |
javascript : event ( add, remove, dispatch, custom event) (0) | 2023.06.08 |
javascript : ecma 2023 updates (0) | 2023.06.08 |
javascript : Array methods that do not mutate the original array (0) | 2023.06.08 |
Comments