[ JavaScript 이벤트 종류 ]
1. 마우스
2. 키보드 입력
3. HTML Frame
4. form & Input
5. touch
[ 용어 정리 ]
1. load : Event Name(Event Type)
2. onload, onclick : Event Attribute
3. function(){...} : Event Listener(Event Handler)
Event Model (Event Name에 Event Listener를 연결하는 방법)
1. DOM Level 0
- Inline Event Model (태그 안에 `onclick='자바스크립트'` 넣는 것)
- Basic Event Model (고전적인 이벤트, 기본 이벤트 처리) - `window.onload = function(){}`
> 이건 이벤트를 하나밖에 못다는게 문제점!
2. DOM Level 2
- Microsoft InternetExplorer Event Model -> attachEvent, detachEvent
- Standard Event Model -> addEventListener, removeEventListener
DOM Level 0 (Inline Event Model)
[1] click event 등록 1 - Inline
inline에 click1와 같이 직접 주거나, click2와 같이 함수로 호출할 수 있다.
<script>/* DOM Level 0 Inline Event Model */varonClick=function(){console.log("clicked2");}</script><body><h1onclick="console.log('clicked1')"> click1 </h1><h1onclick="onClick();"> click2 </h1></body>
[2] click event 등록 2 -window.onload = function(){}
<script>/*
DOM Level 0 Inline Event Model
(고전적인 이벤트, 기본 이벤트 처리) - `window.onload = function(){}`
*/window.onload=function(){varbutton1=document.getElementById('button-1');button1.onclick=function(event){varcounter=document.getElementById('counter-1');varcount=Number(counter.innerText);counter.innerText=""+(count+1);}}</script><body><buttonid="button-1"> button1</button><h1>counter1: <spanid="counter-1">0</span></h1></body>
<script>window.onload=function(){varform=document.getElementById('form-login');// DOM Level0 - 이벤트 기본 동작 막기form.onsubmit=function(){varemail=document.getElementById('email').value;if(email==''){alert('이메일 값은 필수 입력 항목입니다.');document.getElementById('email').focus();returnfalse;}varpassword=document.getElementById('password').value;if(password==''){alert('이메일 값은 필수 입력 항목입니다.');document.getElementById('password').focus();returnfalse;}returntrue;}}</script><body><formid="form-login"method="post"action="login"><label>이메일</label><br><inputtype="text"name="email"id="emial"><br><br><label>비밀번호</label><br><inputtype="text"name="password"id="password"><br><br><inputtype="submit"value="로그인"></form></body>
**2) Level 2 **
<script>window.onload=function(){// DOM Level2 - 이벤트 기본 동작 막기form.addEventListener('submit',function(event){event.preventDefault();varemail=document.getElementById('email').value;console.log(email);if(email==''){alert('이메일은 필수 입력 항목입니다.');document.getElementById('email').focus();return;}varpassword=document.getElementById('password').value;console.log(password);if(password==''){alert('password는 필수 입력 항목입니다.');document.getElementById('password').focus();returnfalse;}return;this.submit();});}</script><body><formid="form-login"method="post"action="login"><label>이메일</label><br><inputtype="text"name="email"id="emial"><br><br><label>비밀번호</label><br><inputtype="text"name="password"id="password"><br><br><inputtype="submit"value="로그인"></form></body>
Event Propagation(Event Bubbling)
이벤트 버블링이란?
특정 요소에서 이벤트가 발생했을 때, 해당 이벤트가 상위의 요소들로 전달되어 가는 특성을 말한다.