DOM#

HTML/XML ๋ฌธ์„œ๋Š” ๋ธŒ๋ผ์šฐ์ € ์•ˆ์—์„œ DOM ํŠธ๋ฆฌ๋กœ ํ‘œํ˜„๋œ๋‹ค ๋ฌธ์„œ๊ฐ์ฒด๋ชจ๋ธ(DOM)์— ๋”ฐ๋ฅด๋ฉด ๋ชจ๋“  HTML tag๋Š” ๊ฐ์ฒด์ด๋‹ค.

๋ฌธ์ž๋“ค๋„ ์—ญ์‹œ ๊ฐ์ฒด๋‹ค. ์ด๋Ÿฐ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ†ตํ•ด์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ณ , ํŽ˜์ด์ง€๋ฅผ ์กฐ์ž‘ํ•  ๋•Œ ์ด ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

html์€ ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ๋˜๊ณ  head,body๋Š” ์ž์‹๋…ธ๋“œ๊ฐ€ ๋˜๊ณ  ๊ทธ ๋ฐ‘์œผ๋กœ ๋˜ ์ž์‹๋“ค์ด ๋“ค๋Ÿฌ๋ถ™๋Š”๋‹ค. ํƒœ๊ทธ๋Š” ์š”์†Œ ๋…ธ๋“œ๊ฐ€ ๋˜๊ณ  ํŠธ๋ฆฌ๊ตฌ์กฐ๋ฅผ ํ˜•์„ฑํ•œ๋‹ค. ๋ฌธ์ž๋Š” ํ…์ŠคํŠธ ๋…ธ๋“œ๊ฐ€ ๋œ๋‹ค. ์ด ์™ธ์— HTML ๋‚ด์˜ ๋ชจ๋“  ๊ฒƒ์€ DOM์„ ๊ตฌ์„ฑํ•œ๋‹ค. ์‹ฌ์ง€์–ด ์ฃผ์„๊นŒ์ง€ ๋œ๋‹ค.

Javascript๋Š” ์ด๋Ÿฌํ•œ DOM์˜ ์š”์†Œ๋“ค์— ๋Œ€ํ•ด์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์–ธ์–ด์ด๊ณ , ๊ทธ ์‹œ์ž‘์€ document๊ฐ์ฒด์—์„œ ์‹œ์ž‘ํ•œ๋‹ค. document ๊ฐ์ฒด๋Š” ๊ทธ ์ž์ฒด๊ฐ€ html ํŽ˜์ด์ง€ ํ•˜๋‚˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

<html> = document.documentElement
<body> = document.body
<head> = document.head

document.body.childNodes๋Š” iterable ์œ ์‚ฌ๋ฐฐ์—ด๊ฐ์ฒด์ธ collection์ด๋‹ค.
ํ•ด์„œ ์ด๊ฑธ๋กœ for๋ฌธ์„ ๋Œ๋ฉด์„œ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•˜๋Š” ์ž‘์—…๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

๋งŒ์•ฝ ๋ฐฐ์—ด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด
Array.from(document.body.childNodes).filter ์™€ ๊ฐ™์€ ์‹์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

DOM ์š”์†Œ ์ฐพ๊ธฐ#

getElementByID, getElementsBy*#

  • ๋’ค์— ๋‚˜์˜ค๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์šฉํ•ด์„œ ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์ฃผ๋Š” ๋ฉ”์„œ๋“œ

  • getElementsBy๋กœ ์ฐพ์•„์˜จ ๊ฒƒ๋“ค์€ live ์ปฌ๋ž™์…˜์ด๋‹ค. ๋ฌธ์„œ์— ๋ณ€๊ฒฝ์ด ์žˆ์„ ๋•Œ๋งˆ๋‹ค ์ž๋™ ๊ฐฑ์‹ ๋˜์–ด ์ตœ์‹  ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•œ๋‹ค.

querySelector ํ‘œ๊ธฐ๋ฒ•#

  1. .class

    • html์š”์†Œ์˜ class ์ด๋ฆ„์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์š”์†Œ๋ฅผ ์„ ํƒํ•˜๋Š” ์„ ํƒ์ž

    • ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง„ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์„ ํƒํ•œ๋‹ค - ๋งˆ์น˜ querySelectorAll(class)์ฒ˜๋Ÿผ

    • ์—ฌ๋Ÿฌ ํด๋ž˜์Šค๋ฅผ ๋™์‹œ์— ์ง€์ •ํ•˜๋ ค๋ฉด ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„

    • ์ €๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ css ํŒŒ์ผ ๋‚ด๋ถ€์—์„œ๋„ ์ •์˜๋œ๋‹ค.

  2. #id

    • id๋Š” document๋‚ด์—์„œ ๊ณ ์œ ํ•ด์•ผํ•œ๋‹ค

    • ๋ณดํ†ต ํ•˜๋‚˜์˜ ์š”์†Œ์— ๋Œ€ํ•ด ๊ณ ์œ ํ•œ ์‹๋ณ„์ž๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

  3. :nth-child(n) elem

    • ์•ž์—์„œ ์„ ํƒํ•œ ์š”์†Œ๋“ค ์ค‘ n๋ฒˆ์งธ๋ฅผ ์„ ํƒ

DOM๊ธฐ๋ฐ˜ node ๊ฒ€์ƒ‰ methods

method

search

์š”์†Œ ํ˜ธ์ถœ

collection๊ฐฑ์‹ 

querySelector

CSS ์„ ํƒ์ž

โœ”

-

querySelectorAll

CSS ์„ ํƒ์ž

โœ”

-

getElementById

id

-

-

getElementsByName

name

-

โœ”

getElementsByTagName

tag or '*'

โœ”

โœ”

getElementsByClassName

class

โœ”

โœ”

์•Œ์•„๋‘๋ฉด ์ข‹์„ methods

method

do

elem.matches(css)

์ผ์น˜ํ•˜๋Š”์ง€ ์—ฌ๋ถ€ ๊ฒ€์‚ฌ

elem.closest(css)

๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์กฐ์ƒ์š”์†Œ ํƒ์ƒ‰

elemA.contains(elemB)

elemB๊ฐ€ elemA์— ์†ํ•˜๊ฑฐ๋‚˜, elemA==elemB์ผ๋•Œ ์ฐธ ๋ฐ˜ํ™˜

DOM ์š”์†Œ ๋ณ€๊ฒฝํ•˜๊ธฐ#

toggle#

DOM์€ ํ•˜๋‚˜์˜ class๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š์„ ํ™•๋ฅ ์ด ๊ต‰์žฅํžˆ ๋†’๋‹ค. ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ ์—†์œผ๋ฉด ๊ทธ ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์žˆ์œผ๋ฉด ์—†์—”๋‹ค(๋งˆ์น˜ ์Šค์œ„์น˜์ฒ˜๋Ÿผ).

const h1 = document.querySelector("div.hello:first-child h1");

function handleTitleClick(){
    // const clickedClass = "clicked";
    // if (h1.classList.contains(clickedClass)){
    //     h1.classList.remove(clickedClass);
    // } else {
    //     h1.classList.add(clickedClass);
    // }
    h1.classList.toggle("clicked")
}
h1.addEventListener("click",handleTitleClick);

referance#