๐ŸŒœ

9. DOM(Document Object Model)

Index

ย 
notion imagenotion image

9. DOM (Document Object Model)

9.1 DOM ์ด๋ž€?

DOM ์€ HTML ๋ฌธ์„œ์˜ ๋‚ด์šฉ์„ ํŠธ๋ฆฌํ˜•ํƒœ๋กœ ๊ตฌ์กฐํ™”ํ•˜์—ฌ ์›นํŽ˜์ด์ง€์™€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ๊ฐ๊ฐ์˜ ์š”์†Œ์™€ ์†์„ฑ, ์ฝ˜ํ…์ธ ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋‹จ์œ„๋ฅผ '๋…ธ๋“œ(node)'๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
notion imagenotion image
ย 
ย 

9.2 DOM ํŠธ๋ฆฌ์— ์ ‘๊ทผํ•˜๊ธฐ

document ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด HTML ๋ฌธ์„œ์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. document๋Š” ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋ถˆ๋Ÿฌ์˜จ ์›นํŽ˜์ด์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, DOM ํŠธ๋ฆฌ์˜ ์ง„์ž…์  ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
// ํ•ด๋‹นํ•˜๋Š” Id๋ฅผ ๊ฐ€์ง„ ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ document.getElementById() // ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ document.getElementsByTagName(); // ํ•ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง„ ๋ชจ๋“  ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ document.getElementsByClassName(); // css ์„ ํƒ์ž๋กœ ๋‹จ์ผ ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ document.querySelector("selector"); // css ์„ ํƒ์ž๋กœ ์—ฌ๋Ÿฌ ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ document.querySelectorAll("selector");
ย 
ย 

9.3 DOM ์ œ์–ด ๋ช…๋ น์–ด

ย 
  1. ์ด๋ฒคํŠธ ์‚ฝ์ž…
target.addEventListener( type, listener )์˜ ๋ฌธ๋ฒ• ํ˜•ํƒœ๋ฅผ ์ง€๋‹™๋‹ˆ๋‹ค.
<button>HELLO!</button>
// ์ด๋ฒคํŠธ์˜ ํƒ€์ž…์—๋Š” click, mouseover, mouseout, wheel ๋“ฑ ๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•ฉ๋‹ˆ๋‹ค. // listener ํ•จ์ˆ˜์˜ ์ธ์ˆ˜์—๋Š” ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์žˆ์Šต๋‹ˆ๋‹ค. const myBtn = document.querySelector("button"); myBtn.addEventListener('click', function(){ console.log("hello world"); })
ย 
  1. ํด๋ž˜์Šค ์ œ์–ด
DOM api๋ฅผ ํ†ตํ•ด ์š”์†Œ์˜ class ์†์„ฑ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
<button>Make me BLUE!</button>
const myBtn = document.querySelector("button"); myBtn.addEventListener('click', function(){ // blue ๋ผ๋Š” ํด๋ž˜์Šค์˜ ์†์„ฑ ๊ฐ’์„ ์ง€์ • ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. myBtn.classList.add("blue"); // myBtn.classList.remove(โ€œblueโ€); ํด๋ž˜์Šค๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. // myBtn.classList.toggle(โ€œblueโ€); ํด๋ž˜์Šค๋ฅผ ํ† ๊ธ€ํ•ฉ๋‹ˆ๋‹ค. ์—†์œผ๋ฉด ๋„ฃ์–ด์ฃผ๊ณ , ์žˆ์œผ๋ฉด ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. // myBtn.classList.contains(โ€œblueโ€); ํ•ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. })
ย 
  1. ์š”์†Œ ์ œ์–ด
DOM api๋ฅผ ์ด์šฉํ•˜๋ฉด ์š”์†Œ๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•˜๊ณ , ์œ„์น˜ํ•˜๊ณ , ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
<ul></ul> <button>Make me MORE!</button>
// document.createElement(target); target ์š”์†Œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. // document.createTextNode(target); target ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. // element.appendChild(target); target ์š”์†Œ๋ฅผ element์˜ ์ž์‹์œผ๋กœ ์œ„์น˜ํ•ฉ๋‹ˆ๋‹ค. // element.removeChild(target); element์˜ target ์ž์‹ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. const myBtn = document.querySelector(โ€œbuttonโ€); const myUl = document.querySelector(โ€œulโ€); myBtn.addEventListener(โ€˜clickโ€™, function(){ for(let i=0; i < 5; i++){ const myLi = document.createElement(โ€˜liโ€™); myUl.appendChild(myLi); } })
<div id="parentElement"> <span id="childElement">hello guys~</span> </div>
// parentElement.insertBefore(target, location); target์š”์†Œ๋ฅผ element์˜ ์ž์‹์ธ location ์œ„์น˜๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. var span = document.createElement("span"); var sibling = document.getElementById("childElement"); var parentDiv = document.getElementById("parentElement"); parentDiv.insertBefore(span, sibling);
ย 
  1. JavaScript ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•ด element, text ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€ํ•˜๊ธฐ
DOM api๋ฅผ ์ด์šฉํ•˜๋ฉด ์š”์†Œ ์•ˆ์˜ ๊ฐ’์— ์ ‘๊ทผํ•˜์—ฌ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜, ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
<p></p> <input type="text"> <button>Write Something!</button>
const myBtn = document.querySelector("button"); const myP = document.querySelector("p"); const myInput = document.querySelector("input"); myBtn.addEventListener('click', function(){ myP.textContent = myInput.value; }); // input ์š”์†Œ์— 'input' ์ด๋ฒคํŠธ๋ฅผ ์—ฐ๊ฒฐํ•˜๋ฉด ์‹ค์‹œ๊ฐ„์œผ๋กœ ๊ฐ’์ด ๋ฐ˜์˜๋˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. myInput.addEventListener('input', ()=>{ myP.textContent = myInput.value; }); myP.innerHTML = "<strong>I'm Strong!!</strong>"; myP.outerHTML = "<div></div>"; // innerHTML ์€ ์š”์†Œ(element) ๋‚ด์— ํฌํ•จ ๋œ HTML ๋งˆํฌ์—…์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. // innerText ์†์„ฑ์€ ์š”์†Œ์™€ ๊ทธ ์ž์†์˜ ๋ Œ๋”๋ง ๋œ ํ…์ŠคํŠธ ์ฝ˜ํ…์ธ ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. (innerText๋Š” "์‚ฌ๋žŒ์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š”" ์š”์†Œ๋งŒ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.) // textContent ์†์„ฑ์€ ๋…ธ๋“œ์˜ ํ…์ŠคํŠธ ์ฝ˜ํ…์ธ ๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.
ย 
** innerHTML ์‚ฌ์šฉ์‹œ ์ฃผ์˜ ์‚ฌํ•ญ : https://developer.mozilla.org/ko/docs/Web/API/Element/innerHTML#security_considerations
** innerText ์™€ textContent์˜ ์ฐจ์ด : https://developer.mozilla.org/ko/docs/Web/API/HTMLElement/innerText#์˜ˆ์ œ
ย 
๋” ์ •๋ฐ€ํ•˜๊ฒŒ ๋ฐฐ์น˜ํ•˜๊ธฐ
<strong class="sayHi"> ๋ฐ˜๊ฐ‘์Šต๋‹ˆ๋‹ค. </strong>
const sayHi = document.querySelector('.sayHi'); sayHi.insertAdjacentHTML('beforebegin', '<span>์•ˆ๋…•ํ•˜์„ธ์š” ์ €๋Š”</span>'); sayHi.insertAdjacentHTML('afterbegin', '<span>์žฌํ˜„์ž…๋‹ˆ๋‹ค</span>'); sayHi.insertAdjacentHTML('beforeend', '<span>๋ฉด์ ‘์˜ค์‹œ๋ฉด</span>'); sayHi.insertAdjacentHTML('afterend', '<span>์น˜ํ‚จ์‚ฌ๋“œ๋ฆด๊ฒŒ์š”</span>');
ย 
ย 
  1. DOM ์•ˆ์—์„œ ๋…ธ๋“œ ํƒ์ƒ‰ํ•˜๊ธฐ
<!-- ์ฃผ์„์ž…๋‹ˆ๋‹ค ์ฃผ์„. --> <article class="cont"> <h1>์•ˆ๋…•ํ•˜์„ธ์š” ์ €๋Š” ์ด๋Ÿฐ ์‚ฌ๋žŒ์ž…๋‹ˆ๋‹ค.</h1> <p>์ง€๊ธˆ๋ถ€ํ„ฐ ์ž๊ธฐ์†Œ๊ฐœ ์˜ฌ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค</p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt incidunt voluptates laudantium fugit, omnis dolore itaque esse exercitationem quam culpa praesentium, quisquam repudiandae aut. Molestias qui quas ea iure officiis. <strong>๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!</strong> </article>
const cont = document.querySelector(".cont"); console.log(cont.firstElementChild); // ์ฒซ๋ฒˆ์งธ ์ž์‹์„ ์ฐพ์Šต๋‹ˆ๋‹ค. console.log(cont.lastElementChild); // ๋งˆ์ง€๋ง‰ ์ž์‹์„ ์ฐพ์Šต๋‹ˆ๋‹ค. console.log(cont.nextElementSibling); // ๋‹ค์Œ ํ˜•์ œ์š”์†Œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. console.log(cont.previousSibling); // ์ด์ „ ํ˜•์ œ์š”์†Œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. console.log(cont.children); // ๋ชจ๋“  ์ง๊ณ„์ž์‹์„ ์ฐพ์Šต๋‹ˆ๋‹ค. console.log(cont.parentElement); // ๋ถ€๋ชจ ์š”์†Œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
ย 

9.4 ์ด๋ฒคํŠธ ๊ฐ์ฒด

์ด๋ฒคํŠธ์—์„œ ํ˜ธ์ถœ๋˜๋Š” ํ•ธ๋“ค๋Ÿฌ์—๋Š” ์ด๋ฒคํŠธ์™€ ๊ด€๋ จ๋œ ๋ชจ๋“  ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ „์†ก๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๋ฐ”๋กœ ์ด๋ฒคํŠธ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค!
<article class="parent"> <ol> <li><button class="btn-first" type="button">๋ฒ„ํŠผ1</button></li> <li><button type="button">๋ฒ„ํŠผ2</button></li> <li><button type="button">๋ฒ„ํŠผ3</button></li> </ol> </article>
const btnFirst = document.querySelector('.btn-first'); btnFirst.addEventListener('click', (event) => { console.log(event); });
ย 

9.5 ์ด๋ฒคํŠธ ํ๋ฆ„

๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์—์„œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ๊ฐ€์žฅ ๋จผ์ € ์ด๋ฒคํŠธ ๋Œ€์ƒ์„ ์ฐพ๊ธฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
notion imagenotion image
์ด๋ฒคํŠธ ๋Œ€์ƒ์„ ์ฐพ์•„๊ฐˆ ๋•Œ ๊ฐ€์žฅ ์ƒ์œ„์˜ window ๊ฐ์ฒด๋ถ€ํ„ฐ document, body ์ˆœ์œผ๋กœ DOM ํŠธ๋ฆฌ๋ฅผ ๋”ฐ๋ผ ๋‚ด๋ ค๊ฐ‘๋‹ˆ๋‹ค. ์ด๋ฅผ ์บก์ฒ˜๋ง ๋‹จ๊ณ„๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ด๋ฒคํŠธ ๋Œ€์ƒ์„ ์ฐพ์•„๊ฐ€๋Š” ๊ณผ์ •์—์„œ ๋ธŒ๋ผ์šฐ์ €๋Š” ์ค‘๊ฐ„์— ๋งŒ๋‚˜๋Š” ๋ชจ๋“  ์บก์ฒ˜๋ง ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฒคํŠธ ๋Œ€์ƒ์„ ์ฐพ๊ณ  ์บก์ฒ˜๋ง์ด ๋๋‚˜๋ฉด ๋‹ค์‹œ DOM ํŠธ๋ฆฌ๋ฅผ ๋”ฐ๋ผ ์˜ฌ๋ผ๊ฐ€๋ฉฐ ๋งŒ๋‚˜๋Š” ๋ชจ๋“  ๋ฒ„๋ธ”๋ง ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ด๋ฒคํŠธ ๋ฒ„๋ธ”๋ง ๋‹จ๊ณ„๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฌํ•œ ๊ณผ์ •์—์„œ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์ฐจ๋ก€๋กœ ์‹คํ–‰๋˜๋Š”๊ฒƒ์„ ์ด๋ฒคํŠธ ์ „ํŒŒ(event propagation)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="../reset.css"> <style> </style> </head> <body> <article class="parent"> <button class="btn" type="button">๋ฒ„ํŠผ</button> </article> <script> const parent = document.querySelector('.parent'); const btnFirst = document.querySelector('.btn'); btnFirst.addEventListener('click', (event) => { console.log("btn capture!"); }) window.addEventListener('click', () => { console.log("window capture!"); }, true); // true : ์บก์ฒ˜๋ง ๋‹จ๊ณ„์˜ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. document.addEventListener('click', () => { console.log("document capture!"); }, true); parent.addEventListener('click', () => { console.log("parent capture!"); }, true); btnFirst.addEventListener('click', (event) => { console.log("btn bubble!"); }) parent.addEventListener('click', () => { console.log("parent bubble!"); }); document.addEventListener('click', () => { console.log("document bubble!"); }); window.addEventListener('click', () => { console.log("window bubble!"); }); </script> </body> </html>
ย 
ย 

9.6 ์ด๋ฒคํŠธ target, currentTarget

์ด๋Ÿฌํ•œ ์ด๋ฒคํŠธ์˜ ํŠน์ง• ๋•๋ถ„์— ์ด๋ฒคํŠธ ๊ฐ์ฒด์—๋Š” target, currentTarget ์ด๋ผ๋Š” ์†์„ฑ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. target ์†์„ฑ์—๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์ง„์›์ง€์˜ ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ ์žˆ์Šต๋‹ˆ๋‹ค. target ์†์„ฑ์„ ํ†ตํ•ด ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์—†๋Š” ์š”์†Œ์˜ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ๋„ ํ•ด๋‹น ์š”์†Œ์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
currentTarget ์†์„ฑ์—๋Š” ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์—ฐ๊ฒฐ๋œ ์š”์†Œ๊ฐ€ ์ฐธ์กฐ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
<article class="parent"> <ol> <li><button class="btn-first" type="button">๋ฒ„ํŠผ1</button></li> <li><button type="button">๋ฒ„ํŠผ2</button></li> <li><button type="button">๋ฒ„ํŠผ3</button></li> </ol> </article> <script> const parent = document.querySelector('.parent'); parent.addEventListener('click', function (event) { console.log(event.target); console.log(event.currentTarget); }) </script>
ย 

9.7 ์ด๋ฒคํŠธ ์œ„์ž„

์•ž์—์„œ ์šฐ๋ฆฌ๋Š” ์ด๋ฒคํŠธ์˜ ํ๋ฆ„์„ ํ†ตํ•ด ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์—†๋Š” ์š”์†Œ์˜ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ๋„ ํ•ด๋‹น ์š”์†Œ์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•ด ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์—†์–ด๋„ ๋งˆ์น˜ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์žˆ๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="../reset.css"> <style> </style> </head> <body> <article class="parent"> <ol> <li><button class="btn-first" type="button">๋ฒ„ํŠผ1</button></li> <li><button type="button">๋ฒ„ํŠผ2</button></li> <li><button type="button">๋ฒ„ํŠผ3</button></li> </ol> </article> <script> const parent = document.querySelector('.parent'); parent.addEventListener('click', function (event) { console.log(event.target); if (event.target.nodeName === "BUTTON") { event.target.innerText = "๋ฒ„ํŠผ4"; } }) </script> </body> </html>
์ด๋Ÿฌํ•œ ํ…Œํฌ๋‹‰์„ ์ด๋ฒคํŠธ ์œ„์ž„์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
ย 

9.8 ์ด๋ฒคํŠธ์˜ this

์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this ๊ฐ’์€ ์ด๋ฒคํŠธ๊ฐ€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
<article class="parent"> <ol> <li><button class="btn-first" type="button">๋ฒ„ํŠผ1</button></li> <li><button type="button">๋ฒ„ํŠผ2</button></li> <li><button type="button">๋ฒ„ํŠผ3</button></li> </ol> </article> <script> const parent = document.querySelector('.parent'); parent.addEventListener('click', function (event) { console.log(this); }) </script>
์ด๋Š” event.currentTarget ์†์„ฑ์˜ ์ฐธ์กฐ๊ฐ’๊ณผ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
๋งŒ์•ฝ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ํ•จ์ˆ˜๋ฅผ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ์“ด๋‹ค๋ฉด this ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ์ด ๋‹ฌ๋ผ์ง„๋‹ค๋Š” ์ ์— ์œ ์˜ํ•˜์„ธ์š”.
ย 

9.9 ์‹ค์Šต

<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="reset.css"> <style> /* ์ง์ ‘ ์…€๋ ‰ํŠธ ๋ฐ•์Šค ๋งŒ๋“ค๊ธฐ */ h2 { margin: 30px; } .cont-select { position: relative; width: 200px; } .btn-select { width: 100%; padding: 13px 14px; color: #000; font-size: 12px; line-height: 14px; text-align: left; border: 1px solid #C4C4C4; box-sizing: border-box; border-radius: 10px; cursor: pointer; background: url("images/icon-Triangle-down.png") right 13px center no-repeat; /* ๋ง์ค„์ž„ ์ถ”๊ฐ€ */ white-space: nowrap; text-overflow: ellipsis; overflow: hidden; } .btn-select:focus { outline: 3px solid #F8E4FF; } .list-member { display: none; width: 100%; overflow: hidden; position: absolute; left: 0; top: 51px; background: #FFF; border: 1px solid #C4C4C4; box-shadow: 4px 4px 14px rgba(0, 0, 0, 0.15); border-radius: 10px; } .btn-select.on { background: url("images/icon-Triangle-up.png") right 13px center no-repeat; } .btn-select.on+.list-member { display: block; } .list-member li { height: 40px; padding: 5px 18px; } .list-member li button { display: block; height: 30px; width: 100%; border: none; background-color: #fff; border-radius: 8px; cursor: pointer; /* ๋ง์ค„์ž„ ์ถ”๊ฐ€ */ white-space: nowrap; text-overflow: ellipsis; overflow: hidden; } .list-member li button:focus, .list-member li button:hover { background-color: #F8E4FF; } </style> </head> <body> <h2>์…€๋ ‰ํŠธ ๋ฐ•์Šค ๋งŒ๋“ค๊ธฐ</h2> <article class="cont-select"> <button class="btn-select">๋‚˜์˜ ์ตœ์•  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด</button> <ul class="list-member"> <!-- <li><button type="button">Python</button></li> <li><button type="button">Java</button></li> <li><button type="button">JavaScript</button></li> <li><button type="button">C#</button></li> <li><button type="button">C/C++</button></li> --> </ul> </article> </body> </html>
ย