div, body,html ์ ๋ชจ๋ click ์ด๋ฒคํธ๊ฐ ์์ ๋
โ currentTarget & target
currentTarget
์ด๋ฒคํธ์ ์ง์ง ์ฃผ์ธ
target
์ด ์ด๋ฒคํธ๊ฐ ๋๊ตฌ๋๋ฌธ์ ์คํ์ด ๋๋๊ฐ
๐ ์ด๋ฒคํธ๋ ์์ฐจ์ ์ผ๋ก ์ฐ์ข์ ๋ก ์คํ๋จ
๋ฐ๋ผ์ ์ด๋ฒคํธ๋ event-flow ๋ฅผ ๊ฐ์ง
โ CapturePhase & BubblePhase
capture phase
porpoagate-up
๋ธ๋ผ์ฐ์ ์์ ๊ฐ์ฅ ๊ฐ๊น์ด๊ฒ๋ถํฐ ์คํ
html > body > div
Target pahse
porpoagate-down
๋น์ฌ์ ์คํ
Bubble phase
propogate-down
๋ค์ ๋ด๋ ค๊ฐ๋ฉฐ ์คํ
์ธ ๋ฐ ์๋ ์ค๋ณต ์ด๋ฒคํธ๊ฐ ์คํ๋๊ฒ ๋จ
html > body > div> body > html
๐ ex) ์์๋ก ๊ณต๋ถํ๊ธฐ
event flow phase ์ค ์ด๋๋จ๊ณ์์ ์คํ๋๊ณ ์ถ์ผ์ง ์ ํ capture vs bubble (๊ธฐ๋ณธ๊ฐ)
๋ฐ๋ผ์ divํด๋ฆญ์
div > body > html
target.addEventListner (' ์ด๋ฒคํธ ํ์ ' , function () {์คํํ ๊ฑฐ } , False(Bubble๋ ์คํ) True(Capture๋ ์คํ))
const body = document.body;
const div = document.querySelector('div');
const html = document.documentElement;
div.addEventListener('click', function(){
console.log("Div");
});
body.addEventListener('click', function(){
console.log("Body");
});
html.addEventListener('click', function(){
console.log("html");
});
1) ๋ชจ๋๋ค bubble์ผ ๋ div click
2) body -> capture & div click
const body = document.body;
const div = document.querySelector('div');
const html = document.documentElement;
div.addEventListener('click', function(){
console.log("Div");
});
body.addEventListener('click', function(){
console.log("Body");
},true);
html.addEventListener('click', function(){
console.log("html");
});
3) html->true ๋ก ํ๊ณ body click
๋๋ฌด๋๋ฌด ์ข์ ๊น๋ฒ๊ทธ๋์ ๊ฐ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ๊ณต๋ถํ์ต๋๋ค๐ฅ
www.youtube.com/watch?v=7gKtNC3b_S8&list=PLsTXYDw7vAcRW9SZnHBeF4HKwATrNi0Gg&index=4
โ event ๊ฐ์ฒด
div.addEventListener('click', function(e){
console.log("Div");
});
e
event ๋ฅผ ๋ฐ์ํ๋ฉด ํ์ ์ ๋ฌด์ ์๊ด์์ด ๊ฐ์ฒด๋ฅผ ๊ผญ ๋ฐํํด ์ค
preventDefault()
default ๋๋ก ํ์ง ๋ง์
โ input ๊ณผ button ์ด๋ฒคํธ ์์
<form action="">
<input type="email" name="email">
<button id="btn" type="submit">์ ์ถ</button>
</form>
<script>
const input = document.querySelector('input');
input.addEventListener('input',function(e){
console.log(e.currentTarget);
console.log(e.currentTarget.value);
})
const btnelm = document.getElementById('btn')
btn.addEventListener('click',function(e){
alert('btn click');
})
</script>
'๐ JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Node.JS express๋ก ์๋ฒ ๊ตฌ์ฑํ๊ธฐ (5) | 2021.02.23 |
---|---|
์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฐ์ฒด (0) | 2021.02.22 |
[JavaScript ] ์ด๋ฒคํธํธ๋ค๋ง (0) | 2021.02.16 |
[JavaScript] DOM์ด๋ ๋ฌด์์ธ๊ฐ? (0) | 2021.02.16 |
์๋ฐ์คํฌ๋ฆฝํธ, ํจ์ํ ํ๋ก๊ทธ๋๋ฐ (0) | 2021.02.08 |
๋๊ธ