DOM

jeongjwonยท2023๋…„ 3์›” 7์ผ
0

SEB FE

๋ชฉ๋ก ๋ณด๊ธฐ
16/56

๐Ÿ“Œ DOM

Document Object Model
xml, html์˜ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๊ตฌ์กฐํ™”๋œ ํ‘œํ˜„์„ ์ œ๊ณตํ•˜์—ฌ ์šฐ๋ฆฌ๋Š” DOM์„ ํ†ตํ•ด ์›นํŽ˜์ด์ง€์˜ ์ปจํ…์ธ , ๊ตฌ์กฐ, ์Šคํƒ€์ผ์„ ์ฝ๊ณ  ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค. nodes์™€ objects๋กœ ๋ฌธ์„œ๋ฅผ ํ‘œํ˜„ํ•˜๊ณ  ์›น ํŽ˜์ด์ง€๋ฅผ ์Šคํฌ๋ฆฝํŠธ ๋˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋“ค์—์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๊ฒŒ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ๋Š” ์—ญ์„ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ DOM์˜ ๊ฐ์ฒด ๊ตฌ์กฐ๋Š” '๋…ธ๋“œ ํŠธ๋ฆฌ' ๋ผ๊ณ ๋„ ๋ถˆ๋ฆฐ๋‹ค. HTML๋กœ ๊ตฌ์„ฑ๋œ ์›น ํŽ˜์ด์ง€๋ฅผ ๋™์ , ์ •์ ์œผ๋กœ ๊ตฌ๋™ ๊ฐ€๋Šฅํ•˜๋‹ค.
HTML ์š”์†Œ(๋ฐฐ์—ด์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ(๋ฐฐ์—ด ์•„๋‹Œ ๋ฐฐ์—ด, ์œ ์‚ฌ๋ฐฐ์—ด, ๋ฐฐ์—ดํ˜• ๊ฐ์ฒด = Array-like Object))

<script> : ๋“ฑ์žฅ๊ณผ ํ•จ๊ป˜ ์‹คํ–‰

document.querySelector(selectors)
document.querySelectorAll(selectors)
document.getElementById(id)
document.getElementByTagName(name)
document.createElement(name)
node.append(node)
node.appendChild(node)
node.remove(node)
node.removeChild(node)
element.innerHTML
node.textContent
element.setAttribute(name, value)
element.getAttribute(name)
element.addEventListener(type, listener)

๐Ÿงฉ innerText vs innerHTML vs textContent

innerTextinnerHTMLtextContent
'element' ์•ˆ์˜ ๋ณด์—ฌ์ง€๋Š” text ๊ฐ’'element' ์•ˆ์˜ HTML์ด๋‚˜ XML'node'๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” text ๊ฐ’
<div id="helloWorld">
  hello World
  <span style="display:none">์ˆจ๊ฒจ์ง„ ํ…์ŠคํŠธ</span>
</div>
<input type='button'
       value='innerHTML'
       onclick='getInnerHTML()'/>
<input type='button'
       value='innerText'
       onclick='getInnerText()'/>
<input type='button'
       value='textContent'
       onclick='getTextContent()'/>
const element = document.querySelector('helloWorld');
function getInnerHTML(){console.log(element.innerHTML);} 
//hello World
//<span style="display:none">์ˆจ๊ฒจ์ง„ ํ…์ŠคํŠธ</span>
function getInnerText(){console.log(element.innerText);} 
//hello World
function getTextContent(){console.log(element.textContent);}   
//hello World
//์ˆจ๊ฒจ์ง„ ํ…์ŠคํŠธ

๐Ÿงฉ DOM vs JavaScript

JavaSciprt ์—†์ด ๋…๋ฆฝ์ ์œผ๋กœ DOM ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„๋งŒ์œผ๋กœ๋„ DOM์„ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.
API = DOM + JavaScript

DOM๊ณผ JavaScript ์ฐจ์ด ์•Œ์•„๋ณด๊ธฐ

๐Ÿงฉ DOM์˜ node ์™€ element

DOM์€ node์˜ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊ฐ ๋…ธ๋“œ๋Š” ๋ถ€๋ชจ์™€ children์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.HTML์— ์žˆ๋Š” tag (<html>, <p>์™€ ๊ฐ™์€...)๋“ค์€ node๋ฅผ ํ‘œํ˜„ํ•˜๊ฒŒ ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ทธ์ € tag, text, ์ฃผ์„์ด๋”๋ผ๋„ node๊ฐ€ ๋œ๋‹ค๋Š” ์ ์ด๋‹ค.
๋ฐ˜๋ฉด element ๋Š” node ์˜ ํ•œ ์ข…๋ฅ˜๋กœ text, ์ฃผ์„์„ ์ œ์™ธํ•œ tag ๋กœ ํ‘œํ˜„๋œ ๋…ธ๋“œ๋ฅผ ๋งํ•œ๋‹ค.

๐Ÿงฉ chilren vs childNodes

childNodes ๋Š” ์ฃผ์–ด์ง„ ์š”์†Œ์˜ ์ž์‹ ๋…ธ๋“œ ๋ชจ์Œ( collection )์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๊ณต๋ฐฑ, ์ฃผ์„, tag, text ๋ฅผ ํฌํ•จํ•œ๋‹ค.
childresn ์€ ์ฃผ์–ด์ง„ ์š”์†Œ์˜ ์ž์‹ html ์—˜๋ฆฌ๋จผํŠธ ๋ชจ์Œ๋งŒ ( collection )์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

//์ค„๋ฐ”๊ฟˆ ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
<div class="test"><!-- Hello!! --><p>Hello World</p> This is text</div> 
//์ค„๋ฐ”๊ฟˆ ํ•œ ๊ฒฝ์šฐ
<div class="test">
  <!-- Hello!! -->
  <p>Hello World</p> 
  This is text
</div>
const test = document.querySelector(".test");
console.log(test.childNodes); 
console.log(text.children);
//childNods >
//์ค„๋ฐ”๊ฟˆ ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
//NodeList(3) = [comment(์ฃผ์„), p, text]
//์ค„๋ฐ”๊ฟˆ ํ•œ ๊ฒฝ์šฐ
//NodeList(5) = [text(๊ณต๋ฐฑ),comment,text(๊ณต๋ฐฑ), p, text]


//children >
//HTMLCollection [p]

๐Ÿงฉ remove vs removeChild

remove๋Š” ๋…ธ๋“œ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์‚ญ์ œํ•˜๊ณ  ์ข…๋ฃŒํ•˜์—ฌ ์ œ๊ฑฐ๋œ ์—˜๋ฆฌ๋จผํŠธ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์˜ GC(Garbage Collection)์— ์˜ํ•ด ๊ณง ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ง€์›Œ์ง„๋‹ค.

removeChild๋Š” ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๋ฉ”๋ชจ๋ฆฌ์— ํ•ด๋‹น ๋…ธ๋“œ๋Š” ๊ทธ๋Œ€๋กœ ์กด์žฌํ•˜๋ฉฐ, ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๋ฅผ ๋Š์–ด DOM ํŠธ๋ฆฌ์—์„œ ํ•ด์ œํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ตœ์ข…์ ์œผ๋กœ ๊ด€๊ณ„๋ฅผ ๋Š์€ ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ฐธ์กฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ฐ˜ํ™˜๊ฐ’์„ ์žฌ์‚ฌ์šฉ ์—ฌ๋ถ€์— ๋”ฐ๋ผ์„œ GC์— ์˜ํ•ด ์ž ์‹œ ํ›„ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์‚ญ์ œ๋˜๊ฑฐ๋‚˜ ๋ฐ˜ํ™˜๋œ ๋…ธ๋“œ ์ฐธ์กฐ๋ฅผ ๋ณ€์ˆ˜์— ๋‹ด์•„ ๋‹ค๋ฅธ DOM ์œ„์น˜์— ๋ถ™์ผ ์ˆ˜๋„ ์žˆ๋‹ค.

// 1. ๋ฐ˜ํ™˜ ๊ฐ’์„ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜์ง€ ์•Š์•„ DOM์—์„œ ์ œ๊ฑฐ๋˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ์—์„œ๋„ ๊ณง ์ง€์›Œ์ง„๋‹ค. 
node.removeChild(child); 
  
// 2. ๋ฐ˜ํ™˜ ๊ฐ’์„ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜์—ฌ DOM์—์„œ ์ œ๊ฑฐ๋˜์ง€๋งŒ ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
let removed = node.removeChild(child);
// document.body.prepend(removed);์˜ ํ˜•ํƒœ๋กœ ๋‹ค์‹œ DOM์— ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค.
document.body.prepend(removed); 

๐Ÿงฉ append vs appendChild

append ๋Š” ์š”์†Œ์— ๋…ธ๋“œ ๊ฐ์ฒด ๋˜๋Š” DOM๋ฌธ์ž์—ด์„ ์ž์‹ ์š”์†Œ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, appendChild ๋Š” ๋…ธ๋“œ ๊ฐ์ฒด๋งŒ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๊ณ , ์ถ”๊ฐ€ํ•œ ์ž์‹ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ๊ฐ€์ง„๋‹ค.

๐Ÿงฉ append vs prepend (vs after vs before)

append ๋Š” ์ปจํ…์ธ ๋ฅผ ์„ ํƒ๋œ ์š”์†Œ ๋‚ด๋ถ€์˜ ๋ ๋ถ€๋ถ„์—์„œ ์‚ฝ์ž…
prepend ๋Š” ์ฝ˜ํ…์ธ ๋ฅผ ์„ ํƒํ•œ ์š”์†Œ ๋‚ด๋ถ€์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์—์„œ ์‚ฝ์ž…
after ๋Š” ์„ ํƒํ•œ ์š”์†Œ ๋’ค์— ์ปจํ…์ธ  ์‚ฝ์ž…
before ๋Š” ์„ ํƒ๋œ ์š”์†Œ ์•ž์— ์ปจํ…์ธ  ์‚ฝ์ž…

// <p> ์›๋ณธ </p>
// p.append('์›๋ณธ ๋’ค'); // <p> ์›๋ณธ ์›๋ณธ ๋’ค </p>
// p.prepend('์›๋ณธ ์•ž'); // <p> ์›๋ณธ ์•ž ์›๋ณธ </p>
// p.after('๋’ค'); // <p> ์›๋ณธ </p> ๋’ค
// p.before('์•ž'); // ์•ž <p> ์›๋ณธ </p>

๐Ÿงฉ ์ƒ๋Œ€์œ„์น˜(Viewport) vs ์ ˆ๋Œ€์œ„์น˜(offsetTop, offsetLeft, offsetWidth, offsetHeight )

https://mommoo.tistory.com/85

0๊ฐœ์˜ ๋Œ“๊ธ€