[WEB] DOM์ด๋ž€?

๐Ÿ“„ DOM์ด๋ž€?

image

DOM์€ Document Object Model์˜ ์ค„์ž„๋ง๋กœ, ์›น ํŽ˜์ด์ง€์˜ ๋ฌธ์„œ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ฒด๋กœ ๋‚˜ํƒ€๋‚ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค.

์›น ํŽ˜์ด์ง€์˜ HTML ๋“ฑ์˜ ์›น ๋ฌธ์„œ๋ฅผ ํŠธ๋ฆฌ ํ˜•ํƒœ๋กœ ํ‘œํ˜„ํ•˜๊ณ  ๊ฐ๊ฐ ์š”์†Œ๋ฅผ ๊ฐ์ฒด๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.

DOM์€ ์›น ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ, ๊ตฌ์กฐ, ์Šคํƒ€์ผ ๋“ฑ์„ ๋™์ ์œผ๋กœ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“„ DOM ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ

โ–ช ํ•˜๋‚˜์˜ ๋…ธ๋“œ ์„ ํƒ

  • document.getElementById
// id๋กœ ํ•˜๋‚˜์˜ ์š”์†Œ๋ฅผ ์„ ํƒํ•œ๋‹ค.
const elem = document.getElementById("one");
// ํด๋ž˜์Šค ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
elem.className = "blue";

console.log(elem); // <li id="one" class="blue">Seoul</li>
console.log(elem.__proto__); // HTMLLIElement
console.log(elem.__proto__.__proto__); // HTMLElement
console.log(elem.__proto__.__proto__.__proto__); // Element
console.log(elem.__proto__.__proto__.__proto__.__proto__); // Node
  • document.querySelector
// CSS ์…€๋ ‰ํ„ฐ๋ฅผ ์ด์šฉํ•ด ์š”์†Œ๋ฅผ ์„ ํƒํ•œ๋‹ค
const elem = document.querySelector("li.red");
// ํด๋ž˜์Šค ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
elem.className = "blue";

โ–ช ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋…ธ๋“œ ์„ ํƒ

  • document.getElementsByClassName
  • document.getElementsByTagName
  • document.querySelectorAll

์ฐธ๊ณ 

Leave a comment