DOM
ํ์ต ํค์๋
DOM
๋ฌธ์๊ฐ์ฒด ์ ํ
๋ฌธ์๊ฐ์ฒด ์กฐ์
Node
์ด๋ฒคํธ
DOM
Document Object Model, ๋ฌธ์ ๊ฐ์ฒด ๋ชจ๋ธ
์น ๋ธ๋ผ์ฐ์ ์ ํ์๋๋ HTML ํ๊ทธ๋ฅผ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ์ดํดํ๊ธฐ ์ํด ๊ฐ์ฒด๋ก ๋ณํ๋ ํํ๋ฅผ ๋งํ๋ค.
์น ๋ธ๋ผ์ฐ์ ์ ํ์๋๋ HTML ํ๊ทธ๋ฅผ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๋ก ๋ณํํ๊ณ ์กฐ์ํ ์ ์๋๋ก API๋ฅผ ์ ๊ณตํ๋ ์ญํ์ ๋ด๋นํ๋ค.
โญ๏ธ DOM์ ํต์ฌ ๋ฌธ์ ๊ฐ์ฒด๋ฅผ ์ ํํ๊ณ ์ ์ด ํ๋ ๊ฒ์ด๋ค.
๋ฌธ์๊ฐ์ฒด ์ ํ
id
์์ฑ (๋จ์ผ)
document.getElementById(id);
class ์ด๋ฆ
์์ฑ (๋ณต์)
document.getElementsByClassName(className);
ํ๊ทธ ์ด๋ฆ
์ฌ์ฉ (๋ณต์)
document.getElementsByTagName(name);
โญ๏ธ
์ ํ์
ํ์ฉ๊ถ์ฅํ๋ ๋ฐฉ์
์ ํ์๋ ํ๊ทธ์ด๋ฆ, ํด๋์ค, id์์ฑ ์ ๋ถ ๊ฐ๋ฅํ๋ค.
document.querySelector(selector); //๋จ์ผ
document.querySelectorAll(selector); //๋ณต์
๋ฌธ์๊ฐ์ฒด ์กฐ์
1. ์คํ์ผ ๋ณ๊ฒฝ : style
style
document.querySelector('p').style.color = 'red';
document.querySelector('p').style.display = 'none';
2. ์์ฑ์ถ๊ฐ : setAttribute
setAttribute
document.querySelector('p').setAttribute('class', 'text');
3. ์์ฑ์ ๊ฑฐ : removeAttribute
removeAttribute
document.querySelector('p').removeAttribute('class');
2. ์์ฑ์ ๊ฑฐ : removeAttribute
removeAttribute
document.querySelector('p').removeAttribute('class');
Last updated