[JavaScript] DOM API : Document Object Model API

2022. 3. 17. 00:29

DOM API์˜ DOM์ด๋ž€ Document Object Model์„ ๋œปํ•˜๊ณ 

์—ฌ๊ธฐ์„œ Document๋Š” HTML๊ณผ ๊ฐ™์€ ๋ฌธ์„œ๋ฅผ ๋œปํ•œ๋‹ค.

๋”ฐ๋ผ์„œ Document Object Model์€ div, span, input๊ณผ ๊ฐ™์€ ํƒœ๊ทธ๋“ค์„ ๋œปํ•œ๋‹ค.

 

DOM API๋ฅผ ์ž‘์„ฑํ•œ jsํŒŒ์ผ HTML์— ์ ์šฉํ•˜๊ธฐ

<script defer src="./main.js"></script>

 

HTML ์š”์†Œ(Element) ๊ฐ€์žฅ ๋จผ์ € ๋‚˜์˜ค๋Š” ํ•˜๋‚˜์˜ ๊ฐ’ ์ฐพ๊ธฐ

const divEl = document.querySelector('div');

 

HTML ์š”์†Œ(Element) ๋ชจ๋‘ ์ฐพ๊ธฐ

const divEls = document.querySelectorAll('div');

 

์ฐพ์€ ์š”์†Œ ๋ฐ˜๋ณตํ•ด์„œ ํ•จ์ˆ˜ ์‹คํ–‰ํ•˜๊ธฐ

divEls.forEach(function(divEl, index) {});

 

Getter, Setter : ๊ฐ’์„ ์–ป๊ฑฐ๋‚˜ ํ• ๋‹นํ•˜๊ธฐ

// Getter
console.log(divEl.textContent);

// Setter
divEl.textContent = 'div Setter';

 

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์„ค์ •ํ•˜๊ธฐ

divEl.addEventListener('click', function() {});

 

์š”์†Œ์˜ ํด๋ž˜์Šค ์ •๋ณด ์ถ”๊ฐ€ํ•˜๊ธฐ

divEl.classList.add('active');

 

์š”์†Œ์˜ ํด๋ž˜์Šค ์ •๋ณด ์‚ญ์ œํ•˜๊ธฐ

divEl.classList.remove('active');

 

์š”์†Œ์˜ ํด๋ž˜์Šค ํฌํ•จ์ƒํƒœ ํ™•์ธํ•˜๊ธฐ

let isContains = boxEl.classList.contains('active');

 

 

** ์ฝ”๋“œ ์‹คํ–‰์€ ๋ธŒ๋ผ์šฐ์ € ์ฝ˜์†”์ฐฝ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅ !!

728x90

BELATED ARTICLES

more