Dom

Jihyun-Jeonยท2022๋…„ 3์›” 29์ผ
0

Javascript - Deep Dive

๋ชฉ๋ก ๋ณด๊ธฐ
11/26

๐Ÿ”ถ Dom

: Dom (=Document Object Model) (=๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ)
: HTML ๋ฌธ์„œ ์ „์ฒด๋ฅผ ๊ฐ์ฒด๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ.
: HTML์„ ๊ณ„์ธตํ™”์‹œ์ผœ ํŠธ๋ฆฌ๊ตฌ์กฐ๋กœ ๋งŒ๋“  ๊ฐ์ฒด(Object) ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค.

Dom tree

(๊ทธ๋ฆผ ์ถœ์ฒ˜: http://www.tcpschool.com/javascript/js_dom_node)

  • ์ด๋•Œ ๊ฐ html์š”์†Œ๋Š” "์š”์†Œ๋…ธ๋“œ"๋กœ ๋ณ€ํ™˜๋œ๋‹ค.
  • ๋…ธ๋“œ๋Š” โ€œ์š”์†Œ ๋…ธ๋“œโ€ , โ€œํ…์ŠคํŠธ ๋…ธ๋“œโ€, โ€œ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๋…ธ๋“œโ€๋กœ ๊ตฌ๋ถ„๋จ
<div class="greeting"> Hello </div>
// <div></div>  โ†์š”์†Œ๋…ธ๋“œ 
// Hello        โ†ํ…์ŠคํŠธ ๋…ธ๋“œ
// class="greeting" โ†์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๋…ธ๋“œ

๐Ÿ”ถ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๋ฒ•

1.

๋ฉ”์†Œ๋“œ์˜๋ฏธ๊ฒฐ๊ณผ
document.getElementById(โ€idโ€)ID์†์„ฑ์œผ๋กœ ํƒœ๊ทธ ์„ ํƒํ•˜๊ธฐID์— ํ•ด๋‹นํ•˜๋Š” ํƒœ๊ทธ ํ•˜๋‚˜
document.getElementsByClassName(โ€classโ€)class ์†์„ฑ์œผ๋กœ ํƒœ๊ทธ ์„ ํƒํ•˜๊ธฐclass์— ํ•ด๋‹นํ•˜๋Š” ํƒœ๊ทธ ๋ชจ์Œ
(HTMLCollection) ์œ ์‚ฌ๋ฐฐ์—ด
document.getElementsByTagName(โ€tagโ€)HTML ํƒœ๊ทธ ์ด๋ฆ„์œผ๋กœ ํƒœ๊ทธ ์„ ํƒํ•˜๊ธฐtag์— ํ•ด๋‹นํ•˜๋Š” ํƒœ๊ทธ ๋ชจ์Œ
(HTMLCollection) ์œ ์‚ฌ๋ฐฐ์—ด
document.querySelector(โ€cssโ€)css ์„ ํƒ์ž๋กœ ํƒœ๊ทธ ์„ ํƒํ•˜๊ธฐcss ์„ ํƒ์ž์— ํ•ด๋‹นํ•˜๋Š” ํƒœ๊ทธ ์ค‘
โ€œ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธโ€ ํƒœ๊ทธ ํ•˜๋‚˜
document.querySelectorAll(โ€cssโ€)css ์„ ํƒ์ž๋กœ ํƒœ๊ทธ ์„ ํƒํ•˜๊ธฐcss ์„ ํƒ์ž์— ํ•ด๋‹นํ•˜๋Š” ํƒœ๊ทธ๋ชจ์Œ
(NodeList)์œ ์‚ฌ๋ฐฐ์—ด

โ€ป getElements , querySelectorAll ์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ๊ฐœ ์š”์†Œ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ โ†’ ์œ ์‚ฌ๋ฐฐ์—ด๋กœ ๋‚˜์˜ด

2.

ํ”„๋กœํผํ‹ฐ์„ค๋ช…
element.children์ž์‹ ๋…ธ๋“œ ์ค‘ ์š”์†Œ ๋…ธ๋“œ๋งŒ HTMLCollection์— ๋‹ด์•„์„œ ๋ฐ˜ํ™˜. (ํ…์ŠคํŠธ ๋…ธ๋“œ๋Š” ํฌํ•จ๋˜์ง€ ์•Š์Œ.)
element.firstElementChild์ฒซ ๋ฒˆ์งธ ์ž์‹ ์š”์†Œ ๋…ธ๋“œ๋งŒ ๋ฐ˜ํ™˜
element.lastElementChild๋งˆ์ง€๋ง‰ ์ž์‹ ์š”์†Œ ๋…ธ๋“œ๋งŒ ๋ฐ˜ํ™˜
element.parentElement๋ถ€๋ชจ ์š”์†Œ ๋…ธ๋“œ๋งŒ ๋ฐ˜ํ™˜
element.previousElementSibling์ž์‹ ์˜ ์ด์ „ ํ˜•์ œ ์š”์†Œ ๋…ธ๋“œ๋งŒ ๋ฐ˜ํ™˜
element.nextElementSibling์ž์‹ ์˜ ๋‹ค์Œ ํ˜•์ œ ์š”์†Œ ๋…ธ๋“œ๋งŒ ๋ฐ˜ํ™˜

โ€ป Node์™€ Element์˜ ์ฐจ์ด

Node.prototype.childNodesElement.prototype.children
Node์˜ ํ”„๋กœํผํ‹ฐ๋Š” ๋Œ€๋ถ€๋ถ„ "ํ…์ŠคํŠธ ๋…ธ๋“œ"์ด๊ฑฐ๋‚˜ "์š”์†Œ ๋…ธ๋“œ"์ž„.Element์˜ ํ”„๋กœํผํ‹ฐ๋Š” ๋‹ค "์š”์†Œ๋…ธ๋“œ" ์ž„.
(ํ…์ŠคํŠธ ๋…ธ๋“œ๋Š” ๋ฐ˜ํ™˜๋˜์ง€ ์•Š์Œ)

* ์‚ฌ์šฉ์˜ˆ์‹œ

   <div id="wrap">
      <h1>Rainbow</h1>
      <ul id="rainbow">
        <li><span>color1 </span>red</li>
        <li><span>color2 </span>orange</li>
        <li><span>color3 </span>yellowgreen</li>
        <li><span>color4 </span>green</li>
        <li><span>color5 </span>blue</li>
        <li><span>color6 </span>darkblue</li>
        <li><span>color7 </span>purple</li>
      </ul>
      <h5>A rainbow is caused by sunlight and atmospheric conditions.</h5>
    </div>
const rainbowEl = document.querySelector("#rainbow");

// 1. element.children ์™€ Node.childNodes
    const ChildEl = rainbowEl.children; // HTMLCollection(7) [li, li, li, li, li, li, li]
    const ChildNd = rainbowEl.childNodes; // NodeList(15) [text, li, text, li, text, li, text, li, text, li, text, li, text, li, text]

// 2. element.firstElementChild ์™€ Node.firstChild
    const firstChildEl = rainbowEl.firstElementChild; // <li><span>color1 </span>red</li>
    const firstNd = rainbowEl.firstChild; // #text

// 3. element.lastElementChild ์™€ Node.lastChild
    const lastEl = rainbowEl.lastElementChild; // <li><span>color7 </span>purple</li>
    const lastNd = rainbowEl.lastChild; // #text

// 4. element.parentElement ์™€ Node.parentNode
    const parentEl = rainbowEl.parentElement; // <div id="wrap"> ... </div>
    const parentNd = rainbowEl.parentNode; // <div id="wrap"> ... </div>

// 5. element.previousElementSibling ์™€ Node.previousSibling
    const prevEl = rainbowEl.previousElementSibling; // 
<h1>Rainbow</h1>
    const prevNd = rainbowEl.previousSibling; // #text

//6. element.nextElementSibling ์™€ Node.nextSibling
    const nextEl = rainbowEl.nextElementSibling; // <h5>...</h5>
    const nextNd = rainbowEl.nextSibling; // #text

๐Ÿ”ถ๋…ธ๋“œ ์ด๋™

1. ๋…ธ๋“œ ์ด๋™

ํ”„๋กœํผํ‹ฐ์„ค๋ช…
prepend์„ ํƒํ•œ์š”์†Œ์˜ ์ž์‹์š”์†Œ ์•ž์— ๋‚ด์šฉ์‚ฝ์ž…
append์„ ํƒํ•œ์š”์†Œ์˜ ์ž์‹์š”์†Œ ๋’ค์— ๋‚ด์šฉ์‚ฝ์ž…
before์„ ํƒํ•œ ์š”์†Œ์˜ ์•ž์— ๋‚ด์šฉ ์‚ฝ์ž…
after์„ ํƒํ•œ ์š”์†Œ์˜ ๋’ค์— ๋‚ด์šฉ ์‚ฝ์ž…

2. ๋…ธ๋“œ ์‚ญ์ œ

ํ”„๋กœํผํ‹ฐ์„ค๋ช…
remove

3. ์‚ฌ์šฉ์˜ˆ์‹œ

    <div id="todos">
      				  <!--before-->
      <ul id="today"> <!--๊ธฐ์ค€-->
       			  	  <!--prepend-->
        <li>๋†€๊ธฐ</li> 
        <li>์ˆจ์‰ฌ๊ธฐ</li>
        <li>์ถค์ถ”๊ธฐ</li>
        			  <!--append-->
      </ul>
      				  <!--after-->
      <hr/>
      
      <ul id="tomorrow">
        <li>์ž ์ž๊ธฐ</li>
        <li>์‚ฐ์ฑ…๊ฐ€๊ธฐ</li>
        <li>์žฅ๋ณด๊ธฐ</li>
      </ul>
    </div>
    /* <๋…ธ๋“œ์ด๋™> */
    const todayEl = document.querySelector('#today');
    const tomorrowEl = document.querySelector('#tomorrow');

    // 1.prepend
    // tomorrow์˜ ์ฒซ๋ฒˆ์งธ li๋ฅผ today์˜ ์ฒซ๋ฒˆ์จฐ li๋กœ ์˜ฎ๊ธฐ๊ธฐ
    todayEl.prepend(tomorrowEl.children[0]);

    // 2.append
    // today์˜ ๋งˆ์ง€๋ง‰ li๋ฅผ tomorrow์˜ ๋งˆ์ง€๋ง‰ li๋กœ ์˜ฎ๊ธฐ๊ธฐ
    tomorrowEl.append(todayEl.children[3]);

    // 3.before
    //today ul์•ž์— h2ํƒœ๊ทธ ๋„ฃ๊ธฐ
    const pTitle = document.createElement('h2');
    pTitle.innerHTML = 'today';
    todayEl.before(pTitle);

    // 4.after
    // tomorrow ul๋’ค์— h2ํƒœ๊ทธ ๋„ฃ๊ธฐ
    const tomoTitle = document.createElement('h2');
    tomoTitle.innerHTML = 'tomorrow';
    tomorrowEl.after(tomoTitle);

    /* <๋…ธ๋“œ์‚ญ์ œ>*/
    // 1.remove()
    // tomoTitle์‚ญ์ œํ•˜๊ธฐ
    tomoTitle.remove();

* append์™€ appendChild์˜ ์ฐจ์ด

appendappendChild
"์—ฌ๋Ÿฌ๊ฐœ" ์ž์‹์š”์†Œ ์„ค์ •๊ฐ€๋Šฅ"ํ•˜๋‚˜๋งŒ" ๊ฐ€๋Šฅ
"html์š”์†Œ" , "๋ฌธ์ž์—ด"์„ ๋„ฃ์Œ"html์š”์†Œ"๋งŒ ๊ฐ€๋Šฅ

๐Ÿ”ถ ์š”์†Œ์˜ ์†์„ฑ ๋‹ค๋ฃจ๊ธฐ

getAttribute, setAttribute, removeAttribute

1.์†์„ฑ ์ถ”๊ฐ€(์ˆ˜์ •)ํ•˜๊ธฐ: element.setAttribute('์†์„ฑ', '๊ฐ’')
2.์†์„ฑ์— ์ ‘๊ทผํ•˜๊ธฐ: element.getAttribute('์†์„ฑ')
3.์†์„ฑ ์ œ๊ฑฐํ•˜๊ธฐ: element.removeAttribute('์†์„ฑ')

const aEl = document.querySelector('a');

// 1.์†์„ฑ ์ถ”๊ฐ€(์ˆ˜์ •)ํ•˜๊ธฐ: element.setAttribute('์†์„ฑ', '๊ฐ’')
  aEl.setAttribute('href', 'http://www.naver.com');

// 2.์†์„ฑ์— ์ ‘๊ทผํ•˜๊ธฐ: element.getAttribute('์†์„ฑ')
  console.log(aEl.getAttribute('href')); // http://www.naver.com

// 3.์†์„ฑ ์ œ๊ฑฐํ•˜๊ธฐ: element.removeAttribute('์†์„ฑ')
  aEl.removeAttribute('href');

๐Ÿ”ถ classList

1. Element.prototype.classlist

: class ์ •๋ณด๋ฅผ ๋‹ด์€ DOMTokenList ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•จ.
โ€ป DOMTokenList๊ฐ์ฒด : ์œ ์‚ฌ๋ฐฐ์—ด, ์ดํ„ฐ๋Ÿฌ๋ธ”์ž„.

2. ๋Œ€ํ‘œ์ ์ธ ๋ฉ”์„œ๋“œ

  • add - ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ 1๊ฐœ ์ด์ƒ์˜ ๋ฌธ์ž์—ด์„ class ๊ฐ’์œผ๋กœ ์ถ”๊ฐ€ํ•จ.
  • remove - ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ 1๊ฐœ ์ด์ƒ์˜ ๋ฌธ์ž์—ด์„ class ๊ฐ’์—์„œ ์‚ญ์ œํ•จ.
  • contains - ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๋ฌธ์ž์—ด๊ณผ ์ผ์น˜ํ•˜๋Š” ํด๋ž˜์Šค๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•จ.
  • replacs - ์ฒซ๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ๋‘๋ฒˆ์จฐ ์ธ์ˆ˜๋กœ ๋ณ€๊ฒฝํ•จ.
  • toggle - ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๋ฌธ์ž์—ด์˜ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜๋ฉด ์ œ๊ฑฐ, ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ถ”๊ฐ€ํ•จ.

3. ์‚ฌ์šฉ์˜ˆ์‹œ

    <ol class="c1">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ol>
    const olEl = document.querySelector('.c1');
  
    // 1.add - ํด๋ž˜์Šค ์ถ”๊ฐ€ํ•˜๊ธฐ
    olEl.classList.add('c2', 'c3');
    console.log(olEl.classList); //  ['c1', 'c2', 'c3]

    // 2.toggle - ํด๋ž˜์Šค ์žˆ์œผ๋ฉด ์‚ญ์ œ, ์—†์œผ๋ฉด ์ถ”๊ฐ€ํ•˜๊ธฐ
    olEl.classList.toggle('c4');
    olEl.classList.toggle('c3');
    console.log(olEl.classList); //  ['c1', 'c2', 'c4']
    
    // 3.remove - ํด๋ž˜์Šค ์‚ญ์ œํ•˜๊ธฐ
    olEl.classList.remove('c4');
    console.log(olEl.classList); //  ['c1', 'c2']

๐Ÿ”ถ innerHTML, textContent

1. innerHTML
1)์กฐํšŒ : HTML ๋งˆํฌ์—…์ด ํฌํ•จ๋œ ๋ฌธ์ž์—ด์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•จ. (text๋ฟ ์•„๋‹ˆ๋ผ , html์š”์†Œ๋„ ๋ฐ˜ํ™˜ํ•จ.)
2) ์ž…๋ ฅ : ๋ฌธ์ž์—ด์ด "์š”์†Œ ๋…ธ๋“œ"๋กœ ๋“ค์–ด๊ฐ.

2. textContent
1)์กฐํšŒ : HTML ๋งˆํฌ์—…์„ ๋ฌด์‹œํ•˜๊ณ  ํ…์ŠคํŠธ๋งŒ ๋ฐ˜ํ™˜ํ•จ.
2)์ž…๋ ฅ : ๋ฌธ์ž์—ด์ด "ํ…์ŠคํŠธ"๋กœ ๊ทธ๋Œ€๋กœ ๋“ค์–ด๊ฐ

<body>
    <div id="hello">
       Hello <span>World</span>
    </div>
</body>
  
const helloEl = document.querySelector('#hello');
// 1.์กฐํšŒ
console.log(helloEl.innerHTML); // Hello <span>World</span>
console.log(helloEl.textContent); // Hello World
// 2.์ž…๋ ฅ
helloEl.innerHTML = '<b>innerHTML</b>'; // <b>innerHTML</b>์ด ์š”์†Œ๋…ธ๋“œ๋กœ ๋“ค์–ด๊ฐ
helloEl.textContent = '<b>innerHTML</b>'; // <b>innerHTML</b> ์ด ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ๋“ค์–ด๊ฐ

๐Ÿ”ถ data- , dataset-

  • html์—์„œ ํƒœํฌ ์†์„ฑ์œผ๋กœ data-**๋ฅผ ์ง€์ •ํ•˜๋ฉด, js์—์„œ dataset-** ์œผ๋กœ ๊ทธ ์†์„ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.

  • data-๋กœ ์‹œ์ž‘ํ•˜๋Š” ์†์„ฑ์€ ๋ชจ๋‘ dataset์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ์— ์ €์žฅ๋จ.
    ex) data-status๋ผ๋Š” ์†์„ฑ์ด ์žˆ๋‹ค๋ฉด, element.dataset.status๋ผ๋Š” ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•ด์„œ ๊ทธ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ž„.

  • (mdn์ž๋ฃŒ)

  • ์ฃผ์˜
    :์ธํ„ฐ๋„ท ์ต์Šคํ”Œ๋กœ๋Ÿฌ11+ ์ด์ „ ๋ฒ„์ „๋“ค์€ dataset์„ ์ง€์›ํ•˜์ง€ ์•Š์Œ.
    IE 10 ์ดํ•˜์—์„  getAttribute()๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ์†์„ฑ์„ ์ ‘๊ทผํ•ด์•ผ ํ•จ.

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