Event(์ด๋ฒคํธ) ๊ฐ๋
- ๋คํธ์ํฌ ํ๋์ด๋ ์ฌ์ฉ์์์ ์ํธ์์ฉ ๊ฐ์ ์ฌ๊ฑด์ ๋ฐ์์ ์๋ฆฌ๊ธฐ ์ํ ๊ฐ์ฒด
- ์ด๋ฒคํธ ๋ฐ์
- ๋ง์ฐ์ค๋ฅผ ํด๋ฆญํ๊ฑฐ๋ ํค๋ณด๋๋ฅผ ๋๋ฅด๋ ๋ฑ ์ฌ์ฉ์ ํ๋์ผ๋ก ๋ฐ์ํ ์๋ ์์
- ํน์ ๋ฉ์๋๋ฅผ ํธ์ถ(Element.click()) ํ์ฌ ํ๋ก๊ทธ๋๋ฐ์ ์ผ๋ก๋ ๋ง๋ค์ด ๋ผ ์ ์์
- AnimationEvent, ClipboardEvent, DragEvent ๋ฑ
- UIEvent
- ๊ฐ๋จํ ์ฌ์ฉ์ ์ธํฐํ์ด์ค ์ด๋ฒคํธ
- Event์ ์์์ ๋ฐ์
- MouseEvent, KeyboardEvent, InputEvent, FocusEvent ๋ฑ์ ๋ถ๋ชจ ๊ฐ์ฒด ์ญํ ์ ํจ
"~ ํ๋ฉด ~ ํ๋ค.", "ํด๋ฆญํ๋ฉด, ๊ฒฝ๊ณ ์ฐฝ์ ๋์ด๋ค.", "ํน์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด, ํ ์ผ์ ๋ฑ๋กํ๋ค."
EventTarget.addEventListner()
- ์ง์ ํ ์ด๋ฒคํธ๊ฐ ๋์์ ์ ๋ฌ๋ ๋๋ง๋ค ํธ์ถํ ํจ์๋ฅผ ์ค์
- ์ด๋ฒคํธ๋ฅผ ์ง์ํ๋ ๋ชจ๋ ๊ฐ์ฒด(Element, Document, Window ๋ฑ)๋ฅผ ๋์์ผ๋ก ์ง์ ๊ฐ๋ฅ
target.addEventListener(type, listener[, options])
- type
- ๋ฐ์ ํ ์ด๋ฒคํธ ์ ํ (๋์๋ฌธ์ ๊ตฌ๋ถ ๋ฌธ์์ด)
- listener
- ์ง์ ๋ ํ์ ์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋ ์๋ฆผ์ ๋ฐ๋ ๊ฐ์ฒด
- EventListener ์ธํฐํ์ด์ค ํน์ JS function ๊ฐ์ฒด(์ฝ๋ฐฑ ํจ์)์ฌ์ผ ํจ
DOM ๊ด๋ จ ๊ฐ์ฒด์ ์์ ๊ตฌ์กฐ ๋ณต์ต
- EventTarget
- Event Listener๋ฅผ ๊ฐ์ง ์ ์๋ ๊ฐ์ฒด๊ฐ ๊ตฌํํ๋ DOM ์ธํฐํ์ด์ค
addEventListner ์์
์ค์ต 1
<buttononclick="alertMessage()">๋๋ฅผ ๋๋ฌ๋ด!</button>constalertMessage=function(){alert('๋ฉ๋กฑ!!!')}์ค์ต 2
<buttonid="my-button">๋๋ฅผ ๋๋ฌ๋ด!!</button>constmyButton=document.querySelector('#my-button')myButton.addEventListener('click',alertMessage)์ค์ต 3
<pid="my-paragraph"></p><formaction="#"><labelfor="my-text-input">๋ด์ฉ์ ์
๋ ฅํ์ธ์.</label><inputid="my-text-input" type="text"></form>constmyTextInput=document.querySelector('#my-text-input')myTextInput.addEventListener('input',function(event){console.log(event)constmyPtag=document.querySelector('#my-paragraph')myPtag.innerText=event.target.value})์ค์ต 4
<h2>Change My Color</h2><labelfor="change-color-input">์ํ๋ ์์์ ์์ด๋ก ์
๋ ฅํ์ธ์.</label><inputid="change-color-input"></input>constcolorInput=document.querySelector('#change-color-input')constchangeColor=function(){consth2Tag=document.querySelector('h2')h2Tag.style.color=event.target.value}colorInput.addEventListener('input',changeColor)Event.preventDefault()
- ํ์ฌ ์ด๋ฒคํธ์ ๊ธฐ๋ณธ ๋์์ ์ค๋จ
- ํ๊ทธ์ ๊ธฐ๋ณธ ๋์์ ์๋ํ์ง ์๊ฒ ๋ง์
- ex) a ํ๊ทธ์ ๊ธฐ๋ณธ ๋์์ ํด๋ฆญ ์ ๋งํฌ๋ก ์ด๋ /form ํ๊ทธ์ ๊ธฐ๋ณธ ๋์์ form ๋ฐ์ดํฐ ์ ์ก
- ์ด๋ฒคํธ๋ฅผ ์ทจ์ํ ์ ์๋ ๊ฒฝ์ฐ, ์ด๋ฒคํธ์ ์ ํ๋ฅผ ๋ง์ง ์๊ณ ๊ทธ ์ด๋ฒคํธ๋ฅผ ์ทจ์
์ค์ต 1
<inputtype="checkbox" id="my-checkbox">constcheckBox=document.querySelector('#my-checkbox')checkBox.addEventListener('click',function(event){event.preventDefault()console.log(event)})์ค์ต 2
<formaction="/articles/" id="my-form"><inputtype="text"><inputtype="submit" value="์ ์ถ!"></form>constformTag=document.querySelector('#my-form')formTag.addEventListener('submit',function(event){console.log(event)event.preventDefault()event.target.reset()})์ค์ต 3
<ahref="https://google.com/" target="_blank" id="my-link">GoToGoogle</a>constaTag=document.querySelector('#my-link')aTag.addEventListener('click',function(event){console.log(event)event.preventDefault()})์ค์ต 4
document.addEventListener('scroll',function(event){console.log(event)event.preventDefault()})โป ์ทจ์ ํ ์ ์๋ ์ด๋ฒคํธ๋ ์กด์ฌ
์ด๋ฒคํธ์ ์ทจ์ ๊ฐ๋ฅ ์ฌ๋ถ๋ event.cancelable์ ์ฌ์ฉํด ํ์ธํ ์ ์์












