Skip to content
- 교재 예제
- ex9-01.html
- ex9-02.html
- ex9-03.html
- ex9-04.html
- ex9-05.html
- ex9-06.html
- ex9-07.html
- ex9-08.html
- ex9-09.html
- ex9-10.html
- ex9-11.html
- ex9-12.html
- ex9-13.html
- ex9-14.html
- ex9-15.html
- ex9-16.html
- ex9-17.html
- ex9-18.html
- ex9-19.html
- ex9-20.html
- ex9-21.html
- fig9-01.html
- fig9-02.html
- 이벤트
- 정의 : 마우스 클릭, 키보드 입력, 이미지나 HTML 문서의 로딩, 타이머의 타임 아웃 등 사용자의 입력 행위나 문서나 브라우저의 상태 변화를 자바스크립트 코드에게 알리는 통지(notification)
- 이벤트 리스너 : 발생한 이벤트에 대처하기 위해 작성된 자바스크립트 코드
- 이벤트 종류
- HTML5에서 이벤트 종류는 70여가지
- 이벤트 리스너 이름은 이벤트 이름 앞에 on을 덧붙임
- 주요 이벤트 리스너
- onkeydown
- onkeypress
- onkeyup
- onclick
- oncontextmenu
- ondblclick
- onmousedown
- onmouseenter
- onmouseleave
- onmousemove
- onmouseover
- onmouseout
- onmouseup
- onwheel
- onabort
- onerror
- onload
- onresize
- onunload
- onbeforeprint
- onafterprint
- onfocus
- onblur
- onchange
- onreset
- onsearch
- onselect
- onsubmit
- 이벤트 리스너 만들기
- HTML 태그 내에 작성
|
<!DOCTYPE html> <html> <head><meta charset="utf-8"></head> <body> <p onmouseover="this.style.backgroundColor='orchid'" onmouseout="this.style.backgroundColor='white'">마우스 이벤트</p> </body> </html> |
- DOM 객체의 이벤트 리스터 프로퍼티에 작성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
<!DOCTYPE html> <html> <head><meta charset="utf-8"> <script> let p; function init() { // 문서가 완전히 로드되었을 때 호출 p = document.getElementById("p"); p.onmouseover = over; // over()를 onmouseover 리스너로 등록 p.onmouseout = out; // out()를 onmouseout 리스너로 등록 } function over() { p.style.backgroundColor="orchid"; } function out() { p.style.backgroundColor="white"; } </script> </head> <body onload="init()"> <p id="p">마우스 이벤트</p> </body> </html> |
- DOM 객체의 addEventListener() 메소드 이용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>addEventListener()를 이용한 리스너 등록</title> <script> let p; function init() { // 문서가 완전히 로드되었을 때 호출 p = document.getElementById("p"); p.addEventListener("mouseover", over); // 이벤트 리스너 등록 p.addEventListener("mouseout", out); // 이벤트 리스너 등록 } function over() { p.style.backgroundColor="orchid"; } function out() { p.style.backgroundColor="white"; } </script> </head> <body onload="init()"> <p id="p">마우스 이벤트</p> </body> </html> |
error: Content is protected !!