6.1 ์ด๋ฒคํธ ๊ฐ์ฒด
JavaScript๋ ๋ค์ํ ์ฌ์ฉ์ ๋์ ๋ฐ ๋ธ๋ผ์ฐ์ ์์ ๋ฐ์ํ๋ ๋ค๋ฅธ ์ด๋ฒคํธ๋ฅผ ์ถ์ ํ ์ ์๋ ๊ฐ๋ ฅํ ์ด๋ฒคํธ ์์คํ ์ ์ ๊ณตํด. ์ด๋ฒคํธ๋ฅผ ๋ค๋ฃฐ ๋ ์ค์ํ ์ธก๋ฉด์ ์ด๋ฒคํธ์ ๋ํ ์ ๋ณด๋ฅผ ํฌํจํ๊ณ ์ฒ๋ฆฌ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ ์ด๋ฒคํธ ๊ฐ์ฒด์ด์ผ.
์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด ๋ธ๋ผ์ฐ์ ๋ ํด๋น ์ด๋ฒคํธ์ ๋ํ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ์ด๋ฒคํธ ๊ฐ์ฒด๋ฅผ ์์ฑํด. ์ด ๊ฐ์ฒด๋ ์ด๋ฒคํธ ํธ๋ค๋ฌ์ ์ธ์๋ก ์ ๋ฌ๋ผ.
์ด๋ฒคํธ ๊ฐ์ฒด์ ์์ฑ:
type
: ์ด๋ฒคํธ์ ์ ํ (์:click
,keydown
).target
: ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ์์.currentTarget
: ์ด๋ฒคํธ ํธ๋ค๋ฌ๊ฐ ์ฐ๊ฒฐ๋ ์์.eventPhase
: ์ด๋ฒคํธ์ ๋จ๊ณ (์บก์ฒ, ํ๊ฒ, ๋ฒ๋ธ๋ง).bubbles
: ์ด๋ฒคํธ๊ฐ ๋ฒ๋ธ๋ง๋ ์ ์๋์ง๋ฅผ ๋ํ๋ด๋ ๋ ผ๋ฆฌ ๊ฐ.cancelable
: ์ด๋ฒคํธ๋ฅผ ์ทจ์ํ ์ ์๋์ง๋ฅผ ๋ํ๋ด๋ ๋ ผ๋ฆฌ ๊ฐ.defaultPrevented
: ๊ธฐ๋ณธ ๋์์ด ๋ฐฉ์ง๋์๋์ง๋ฅผ ๋ํ๋ด๋ ๋ ผ๋ฆฌ ๊ฐ.timeStamp
: ์ด๋ฒคํธ๊ฐ ์์ฑ๋ ์๊ฐ.isTrusted
: ์ด๋ฒคํธ๊ฐ ์ฌ์ฉ์์ ์ํด ์์ฑ๋์๋์ง ์คํฌ๋ฆฝํธ์ ์ํด ์์ฑ๋์๋์ง๋ฅผ ๋ํ๋ด๋ ๋ ผ๋ฆฌ ๊ฐ.
์ด๋ฒคํธ ๊ฐ์ฒด์ ๋ฉ์๋:
preventDefault()
: ์ด๋ฒคํธ์ ๊ด๋ จ๋ ๊ธฐ๋ณธ ๋์์ ์ทจ์ํด.stopPropagation()
: ์ด๋ฒคํธ์ ์ถ๊ฐ ์ ํ๋ฅผ ์ค์งํด.stopImmediatePropagation()
: ์ด๋ฒคํธ์ ์ถ๊ฐ ์ ํ๋ฅผ ์ค์งํ๊ณ ํ์ฌ ์์์ ๋ํ ๋ค๋ฅธ ์ด๋ฒคํธ ํธ๋ค๋ฌ์ ์คํ์ ๋ฐฉ์งํด.
์ด๋ฒคํธ ๊ฐ์ฒด ์ฌ์ฉ ์์ :
<!DOCTYPE html>
<html>
<head>
<title>Event Object Example</title>
</head>
<body>
<button id="myButton">Click me</button>
<script>
const button = document.getElementById('myButton');
button.addEventListener('click', function(event) {
console.log('Event type:', event.type); // ์ถ๋ ฅ: click
console.log('Event target:', event.target); // ์ถ๋ ฅ: <button id="myButton">Click me</button>
console.log('Current target:', event.currentTarget); // ์ถ๋ ฅ: <button id="myButton">Click me</button>
console.log('Event phase:', event.eventPhase); // ์ถ๋ ฅ: 2 (ํ๊ฒ ๋จ๊ณ)
console.log('Bubbles:', event.bubbles); // ์ถ๋ ฅ: true
console.log('Cancelable:', event.cancelable); // ์ถ๋ ฅ: true
console.log('Time stamp:', event.timeStamp); // ์ถ๋ ฅ: ๋ฐ๋ฆฌ์ด ๋จ์ ์๊ฐ
console.log('Is trusted:', event.isTrusted); // ์ถ๋ ฅ: true
});
</script>
</body>
</html>
6.2. ๋ง์ฐ์ค ์ด๋ฒคํธ (Mouse Events)
๋ง์ฐ์ค ์ด๋ฒคํธ๋ ์ฌ์ฉ์๊ฐ ๋ง์ฐ์ค๋ฅผ ์ฌ์ฉํ์ฌ ์์์ ์ํธ ์์ฉํ ๋ ์์ฑ๋ผ:
click
: ํด๋ฆญ ์ด๋ฒคํธdblclick
: ๋๋ธ ํด๋ฆญ ์ด๋ฒคํธmousedown
: ๋ง์ฐ์ค ๋ฒํผ์ ๋๋ฅผ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธmouseup
: ๋ง์ฐ์ค ๋ฒํผ์ ๋ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธmousemove
: ๋ง์ฐ์ค ์ด๋ ์ด๋ฒคํธmouseover
: ๋ง์ฐ์ค๋ฅผ ์ฌ๋ ธ์ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธmouseout
: ๋ง์ฐ์ค๊ฐ ๋๊ฐ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธ
์์ :
<!DOCTYPE html>
<html>
<head>
<title>Mouse Events Example</title>
</head>
<body>
<button id="myButton">Click Me</button>
<script>
const button = document.getElementById('myButton');
button.addEventListener('click', function(event) {
console.log('Button clicked');
console.log('Event type:', event.type);
console.log('Target element:', event.target);
});
</script>
</body>
</html>
6.3 ๋ก๋ ์ด๋ฒคํธ (Load Events)
๋ก๋ ์ด๋ฒคํธ๋ ๋ฆฌ์์ค๊ฐ ๋ก๋๋ฅผ ์๋ฃํ ๋ ๋ฐ์ํด:
load
: ๋ฆฌ์์ค/ํ์ด์ง๊ฐ ์์ ํ ๋ก๋๋์์ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธDOMContentLoaded
: ์ด๊ธฐ HTML ๋ฌธ์๊ฐ ๋ก๋๋๊ณ ์คํ์ผ ์ํธ, ์ด๋ฏธ์ง ๋ฐ ํ์ ํ๋ ์์ ์์ ํ ๋ก๋๋ฅผ ๊ธฐ๋ค๋ฆฌ์ง ์๊ณ ๊ตฌ๋ฌธ ๋ถ์๋ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธ
์์ :
<!DOCTYPE html>
<html>
<head>
<title>Load Events Example</title>
</head>
<body>
<script>
window.addEventListener('load', function(event) {
console.log('Window fully loaded');
});
document.addEventListener('DOMContentLoaded', function(event) {
console.log('DOM fully loaded and parsed');
});
</script>
</body>
</html>
6.4 ํฌ์ปค์ค ์ด๋ฒคํธ (Focus Events)
ํฌ์ปค์ค ์ด๋ฒคํธ๋ ์์๊ฐ ํฌ์ปค์ค๋ฅผ ๋ฐ๊ฑฐ๋ ์์ ๋ ์์ฑ๋ผ.
focus
: ์์๊ฐ ํฌ์ปค์ค๋ฅผ ๋ฐ์ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธblur
: ์์๊ฐ ํฌ์ปค์ค๋ฅผ ์์ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธ
์์ :
<!DOCTYPE html>
<html>
<head>
<title>Focus Events Example</title>
</head>
<body>
<input type="text" id="textInput" placeholder="Type something...">
<script>
const input = document.getElementById('textInput');
input.addEventListener('focus', function(event) {
console.log('Input field focused');
});
input.addEventListener('blur', function(event) {
console.log('Input field lost focus');
});
</script>
</body>
</html>
6.5 ํค๋ณด๋ ์ด๋ฒคํธ (Keyboard Events)
ํค๋ณด๋ ์ด๋ฒคํธ
๋ ์ฌ์ฉ์๊ฐ ํค๋ณด๋์์ ํค๋ฅผ ๋๋ ์ ๋ ์์ฑ๋ผ:
keydown
: ํค๊ฐ ๋๋ ธ์ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธkeyup
: ํค๊ฐ ๋ฆด๋ฆฌ์ค๋์์ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธkeypress
: ํค๊ฐ ๋๋ฆฌ๊ณ ๋ฆด๋ฆฌ์ค๋ ๋ ๋ฐ์ํ๋ ์ด๋ฒคํธ (๊ตฌ์, ์ฌ์ฉ ๊ถ์ฅํ์ง ์์)
์์ :
<html>
<head>
<title>Keyboard Events Example</title>
</head>
<body>
<input type="text" id="textInput" placeholder="Type something...">
<script>
const input = document.getElementById('textInput');
input.addEventListener('keydown', function(event) {
console.log('Key down:', event.key);
});
input.addEventListener('keyup', function(event) {
console.log('Key up:', event.key);
});
</script>
</body>
</html>
GO TO FULL VERSION