Skip to content

Latest commit

ย 

History

History
1915 lines (1589 loc) ยท 46.3 KB

File metadata and controls

1915 lines (1589 loc) ยท 46.3 KB

39-01

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    <ul>
      <li id="apple">Apple</li>
      <li id="banana">Banana</li>
      <li id="orange">Orange</li>
    </ul>
    <script src="app.js"></script>
  </body>
</html>

39-02

<!DOCTYPE html>
<html>
<body>
  <input type="text">
  <script>
    // input ์š”์†Œ ๋…ธ๋“œ ๊ฐ์ฒด๋ฅผ ์„ ํƒ
    const $input = document.querySelector('input');

    // input ์š”์†Œ ๋…ธ๋“œ ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ
    console.log(
      Object.getPrototypeOf($input) === HTMLInputElement.prototype,
      Object.getPrototypeOf(HTMLInputElement.prototype) === HTMLElement.prototype,
      Object.getPrototypeOf(HTMLElement.prototype) === Element.prototype,
      Object.getPrototypeOf(Element.prototype) === Node.prototype,
      Object.getPrototypeOf(Node.prototype) === EventTarget.prototype,
      Object.getPrototypeOf(EventTarget.prototype) === Object.prototype
    ); // ๋ชจ๋‘ true
  </script>
</body>
</html>

39-03

<!DOCTYPE html>
<html>
  <body>
    <ul>
      <li id="apple">Apple</li>
      <li id="banana">Banana</li>
      <li id="orange">Orange</li>
    </ul>
    <script>
      // id ๊ฐ’์ด 'banana'์ธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      // ๋‘ ๋ฒˆ์งธ li ์š”์†Œ๊ฐ€ ํŒŒ์‹ฑ๋˜์–ด ์ƒ์„ฑ๋œ ์š”์†Œ ๋…ธ๋“œ๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.
      const $elem = document.getElementById('banana');

      // ์ทจ๋“ํ•œ ์š”์†Œ ๋…ธ๋“œ์˜ style.color ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
      $elem.style.color = 'red';
    </script>
  </body>
</html>

39-04

<!DOCTYPE html>
<html>
  <body>
    <ul>
      <li id="banana">Apple</li>
      <li id="banana">Banana</li>
      <li id="banana">Orange</li>
    </ul>
    <script>
      // getElementById ๋ฉ”์„œ๋“œ๋Š” ์–ธ์ œ๋‚˜ ๋‹จ ํ•˜๋‚˜์˜ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      // ์ฒซ ๋ฒˆ์งธ li ์š”์†Œ๊ฐ€ ํŒŒ์‹ฑ๋˜์–ด ์ƒ์„ฑ๋œ ์š”์†Œ ๋…ธ๋“œ๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.
      const $elem = document.getElementById('banana');

      // ์ทจ๋“ํ•œ ์š”์†Œ ๋…ธ๋“œ์˜ style.color ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
      $elem.style.color = 'red';
    </script>
  </body>
</html>

39-05

<!DOCTYPE html>
<html>
  <body>
    <ul>
      <li id="apple">Apple</li>
      <li id="banana">Banana</li>
      <li id="orange">Orange</li>
    </ul>
    <script>
      // id ๊ฐ’์ด 'grape'์ธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค. null์ด ๋ฐ˜ํ™˜๋œ๋‹ค.
      const $elem = document.getElementById('grape');

      // ์ทจ๋“ํ•œ ์š”์†Œ ๋…ธ๋“œ์˜ style.color ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
      $elem.style.color = 'red';
      // -> TypeError: Cannot read property 'style' of null
    </script>
  </body>
</html>

39-06

<!DOCTYPE html>
<html>
  <body>
    <div id="foo"></div>
    <script>
      // id ๊ฐ’๊ณผ ๋™์ผํ•œ ์ด๋ฆ„์˜ ์ „์—ญ ๋ณ€์ˆ˜๊ฐ€ ์•”๋ฌต์ ์œผ๋กœ ์„ ์–ธ๋˜๊ณ  ํ•ด๋‹น ๋…ธ๋“œ ๊ฐ์ฒด๊ฐ€ ํ• ๋‹น๋œ๋‹ค.
      console.log(foo === document.getElementById('foo')); // true

      // ์•”๋ฌต์  ์ „์—ญ์œผ๋กœ ์ƒ์„ฑ๋œ ์ „์—ญ ํ”„๋กœํผํ‹ฐ๋Š” ์‚ญ์ œ๋˜์ง€๋งŒ ์ „์—ญ ๋ณ€์ˆ˜๋Š” ์‚ญ์ œ๋˜์ง€ ์•Š๋Š”๋‹ค.
      delete foo;
      console.log(foo); // <div id="foo"></div>
    </script>
  </body>
</html>

39-07

<!DOCTYPE html>
<html>
  <body>
    <div id="foo"></div>
    <script>
      let foo = 1;

      // id ๊ฐ’๊ณผ ๋™์ผํ•œ ์ด๋ฆ„์˜ ์ „์—ญ ๋ณ€์ˆ˜๊ฐ€ ์ด๋ฏธ ์„ ์–ธ๋˜์–ด ์žˆ์œผ๋ฉด ๋…ธ๋“œ ๊ฐ์ฒด๊ฐ€ ์žฌํ• ๋‹น๋˜์ง€ ์•Š๋Š”๋‹ค.
      console.log(foo); // 1
    </script>
  </body>
</html>

39-08

<!DOCTYPE html>
<html>
  <body>
    <ul>
      <li id="apple">Apple</li>
      <li id="banana">Banana</li>
      <li id="orange">Orange</li>
    </ul>
    <script>
      // ํƒœ๊ทธ ์ด๋ฆ„์ด li์ธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      // ํƒ์ƒ‰๋œ ์š”์†Œ ๋…ธ๋“œ๋“ค์€ HTMLCollection ๊ฐ์ฒด์— ๋‹ด๊ฒจ ๋ฐ˜ํ™˜๋œ๋‹ค.
      // HTMLCollection ๊ฐ์ฒด๋Š” ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด์ด๋ฉด์„œ ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋‹ค.
      const $elems = document.getElementsByTagName('li');

      // ์ทจ๋“ํ•œ ๋ชจ๋“  ์š”์†Œ ๋…ธ๋“œ์˜ style.color ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
      // HTMLCollection ๊ฐ์ฒด๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ˆœํšŒํ•˜๋ฉฐ color ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
      [...$elems].forEach(elem => { elem.style.color = 'red'; });
    </script>
  </body>
</html>

39-09

// ๋ชจ๋“  ์š”์†Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
const $all = document.getElementsByTagName('*');
// -> HTMLCollection(8)ย [html, head, body, ul, li#apple, li#banana, li#orange, script, apple: li#apple, banana: li#banana, orange: li#orange]

39-10

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li>Apple</li>
      <li>Banana</li>
      <li>Orange</li>
    </ul>
    <ul>
      <li>HTML</li>
    </ul>
    <script>
      // DOM ์ „์ฒด์—์„œ ํƒœ๊ทธ ์ด๋ฆ„์ด li์ธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      const $lisFromDocument = document.getElementsByTagName('li');
      console.log($lisFromDocument); // HTMLCollection(4) [li, li, li, li]

      // #fruits ์š”์†Œ์˜ ์ž์† ๋…ธ๋“œ ์ค‘์—์„œ ํƒœ๊ทธ ์ด๋ฆ„์ด li์ธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘
      // ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      const $fruits = document.getElementById('fruits');
      const $lisFromFruits = $fruits.getElementsByTagName('li');
      console.log($lisFromFruits); // HTMLCollection(3) [li, li, li]
    </script>
  </body>
</html>

39-11

<!DOCTYPE html>
<html>
  <body>
    <ul>
      <li class="fruit apple">Apple</li>
      <li class="fruit banana">Banana</li>
      <li class="fruit orange">Orange</li>
    </ul>
    <script>
      // class ๊ฐ’์ด 'fruit'์ธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜์—ฌ HTMLCollection ๊ฐ์ฒด์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      const $elems = document.getElementsByClassName('fruit');

      // ์ทจ๋“ํ•œ ๋ชจ๋“  ์š”์†Œ์˜ CSS color ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
      [...$elems].forEach(elem => { elem.style.color = 'red'; });

      // class ๊ฐ’์ด 'fruit apple'์ธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜์—ฌ HTMLCollection ๊ฐ์ฒด์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      const $apples = document.getElementsByClassName('fruit apple');

      // ์ทจ๋“ํ•œ ๋ชจ๋“  ์š”์†Œ ๋…ธ๋“œ์˜ style.color ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
      [...$apples].forEach(elem => { elem.style.color = 'blue'; });
    </script>
  </body>
</html>

39-12

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li class="apple">Apple</li>
      <li class="banana">Banana</li>
      <li class="orange">Orange</li>
    </ul>
    <div class="banana">Banana</div>
    <script>
      // DOM ์ „์ฒด์—์„œ class ๊ฐ’์ด 'banana'์ธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      const $bananasFromDocument = document.getElementsByClassName('banana');
      console.log($bananasFromDocument); // HTMLCollection(2) [li.banana, div.banana]

      // #fruits ์š”์†Œ์˜ ์ž์† ๋…ธ๋“œ ์ค‘์—์„œ class ๊ฐ’์ด 'banana'์ธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      const $fruits = document.getElementById('fruits');
      const $bananasFromFruits = $fruits.getElementsByClassName('banana');

      console.log($bananasFromFruits); // HTMLCollection [li.banana]
    </script>
  </body>
</html>

39-13

/* ์ „์ฒด ์„ ํƒ์ž: ๋ชจ๋“  ์š”์†Œ๋ฅผ ์„ ํƒ */
* { ... }
/* ํƒœ๊ทธ ์„ ํƒ์ž: ๋ชจ๋“  p ํƒœ๊ทธ ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒ */
p { ... }
/* id ์„ ํƒ์ž: id ๊ฐ’์ด 'foo'์ธ ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒ */
#foo { ... }
/* class ์„ ํƒ์ž: class ๊ฐ’์ด 'foo'์ธ ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒ */
.foo { ... }
/* ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์„ ํƒ์ž: input ์š”์†Œ ์ค‘์— type ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’์ด 'text'์ธ ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒ */
input[type=text] { ... }
/* ํ›„์† ์„ ํƒ์ž: div ์š”์†Œ์˜ ํ›„์† ์š”์†Œ ์ค‘ p ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒ */
div p { ... }
/* ์ž์‹ ์„ ํƒ์ž: div ์š”์†Œ์˜ ์ž์‹ ์š”์†Œ ์ค‘ p ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒ */
div > p { ... }
/* ์ธ์ ‘ ํ˜•์ œ ์„ ํƒ์ž: p ์š”์†Œ์˜ ํ˜•์ œ ์š”์†Œ ์ค‘์— p ์š”์†Œ ๋ฐ”๋กœ ๋’ค์— ์œ„์น˜ํ•˜๋Š” ul ์š”์†Œ๋ฅผ ์„ ํƒ */
p + ul { ... }
/* ์ผ๋ฐ˜ ํ˜•์ œ ์„ ํƒ์ž: p ์š”์†Œ์˜ ํ˜•์ œ ์š”์†Œ ์ค‘์— p ์š”์†Œ ๋’ค์— ์œ„์น˜ํ•˜๋Š” ul ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒ */
p ~ ul { ... }
/* ๊ฐ€์ƒ ํด๋ž˜์Šค ์„ ํƒ์ž: hover ์ƒํƒœ์ธ a ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒ */
a:hover { ... }
/* ๊ฐ€์ƒ ์š”์†Œ ์„ ํƒ์ž: p ์š”์†Œ์˜ ์ฝ˜ํ…์ธ ์˜ ์•ž์— ์œ„์น˜ํ•˜๋Š” ๊ณต๊ฐ„์„ ์„ ํƒ
   ์ผ๋ฐ˜์ ์œผ๋กœ content ํ”„๋กœํผํ‹ฐ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋œ๋‹ค. */
p::before { ... }

39-14

<!DOCTYPE html>
<html>
  <body>
    <ul>
      <li class="apple">Apple</li>
      <li class="banana">Banana</li>
      <li class="orange">Orange</li>
    </ul>
    <script>
      // class ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’์ด 'banana'์ธ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      const $elem = document.querySelector('.banana');

      // ์ทจ๋“ํ•œ ์š”์†Œ ๋…ธ๋“œ์˜ style.color ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
      $elem.style.color = 'red';
    </script>
  </body>
</html>

39-15

<!DOCTYPE html>
<html>
  <body>
    <ul>
      <li class="apple">Apple</li>
      <li class="banana">Banana</li>
      <li class="orange">Orange</li>
    </ul>
    <script>
      // ul ์š”์†Œ์˜ ์ž์‹ ์š”์†Œ์ธ li ์š”์†Œ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      const $elems = document.querySelectorAll('ul > li');
      // ์ทจ๋“ํ•œ ์š”์†Œ ๋…ธ๋“œ๋“ค์€ NodeList ๊ฐ์ฒด์— ๋‹ด๊ฒจ ๋ฐ˜ํ™˜๋œ๋‹ค.
      console.log($elems); // NodeList(3) [li.apple, li.banana, li.orange]

      // ์ทจ๋“ํ•œ ๋ชจ๋“  ์š”์†Œ ๋…ธ๋“œ์˜ style.color ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
      // NodeList๋Š” forEach ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
      $elems.forEach(elem => { elem.style.color = 'red'; });
    </script>
  </body>
</html>

39-16

// ๋ชจ๋“  ์š”์†Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
const $all = document.querySelectorAll('*');
// -> NodeList(8) [html, head, body, ul, li#apple, li#banana, li#orange, script]

39-17

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li class="apple">Apple</li>
      <li class="banana">Banana</li>
      <li class="orange">Orange</li>
    </ul>
  </body>
  <script>
    const $apple = document.querySelector('.apple');

    // $apple ๋…ธ๋“œ๋Š” '#fruits > li.apple'๋กœ ์ทจ๋“ํ•  ์ˆ˜ ์žˆ๋‹ค.
    console.log($apple.matches('#fruits > li.apple'));  // true

    // $apple ๋…ธ๋“œ๋Š” '#fruits > li.banana'๋กœ ์ทจ๋“ํ•  ์ˆ˜ ์—†๋‹ค.
    console.log($apple.matches('#fruits > li.banana')); // false
  </script>
</html>

39-18

<!DOCTYPE html>
<head>
  <style>
    .red { color: red; }
    .blue { color: blue; }
  </style>
</head>
<html>
  <body>
    <ul id="fruits">
      <li class="red">Apple</li>
      <li class="red">Banana</li>
      <li class="red">Orange</li>
    </ul>
    <script>
      // class ๊ฐ’์ด 'red'์ธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜์—ฌ HTMLCollection ๊ฐ์ฒด์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
      const $elems = document.getElementsByClassName('red');
      // ์ด ์‹œ์ ์— HTMLCollection ๊ฐ์ฒด์—๋Š” 3๊ฐœ์˜ ์š”์†Œ ๋…ธ๋“œ๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋‹ค.
      console.log($elems); // HTMLCollection(3) [li.red, li.red, li.red]

      // HTMLCollection ๊ฐ์ฒด์˜ ๋ชจ๋“  ์š”์†Œ์˜ class ๊ฐ’์„ 'blue'๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.
      for (let i = 0; i < $elems.length; i++) {
        $elems[i].className = 'blue';
      }

      // HTMLCollection ๊ฐ์ฒด์˜ ์š”์†Œ๊ฐ€ 3๊ฐœ์—์„œ 1๊ฐœ๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค.
      console.log($elems); // HTMLCollection(1) [li.red]
    </script>
  </body>
</html>

39-19

// for ๋ฌธ์„ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ์ˆœํšŒ
for (let i = $elems.length - 1; i >= 0; i--) {
  $elems[i].className = 'blue';
}

39-20

// while ๋ฌธ์œผ๋กœ HTMLCollection์— ์š”์†Œ๊ฐ€ ๋‚จ์•„ ์žˆ์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ ๋ฌดํ•œ ๋ฐ˜๋ณต
let i = 0;
while ($elems.length > i) {
  $elems[i].className = 'blue';
}

39-21

// ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด์ด๋ฉด์„œ ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ HTMLCollection์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ˆœํšŒ
[...$elems].forEach(elem => elem.className = 'blue');

39-22

// querySelectorAll์€ DOM ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด์ธ NodeList๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
const $elems = document.querySelectorAll('.red');

// NodeList ๊ฐ์ฒด๋Š” NodeList.prototype.forEach ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
$elems.forEach(elem => elem.className = 'blue');

39-23

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li>Apple</li>
      <li>Banana</li>
    </ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // childNodes ํ”„๋กœํผํ‹ฐ๋Š” NodeList ๊ฐ์ฒด(live)๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    const { childNodes } = $fruits;
    console.log(childNodes instanceof NodeList); // true

    // $fruits ์š”์†Œ์˜ ์ž์‹ ๋…ธ๋“œ๋Š” ๊ณต๋ฐฑ ํ…์ŠคํŠธ ๋…ธ๋“œ(39.3.1์ ˆ "๊ณต๋ฐฑ ํ…์ŠคํŠธ ๋…ธ๋“œ" ์ฐธ๊ณ )๋ฅผ ํฌํ•จํ•ด ๋ชจ๋‘ 5๊ฐœ๋‹ค.
    console.log(childNodes); // NodeList(5) [text, li, text, li, text]

    for (let i = 0; i < childNodes.length; i++) {
      // removeChild ๋ฉ”์„œ๋“œ๋Š” $fruits ์š”์†Œ์˜ ์ž์‹ ๋…ธ๋“œ๋ฅผ DOM์—์„œ ์‚ญ์ œํ•œ๋‹ค.
      // (39.6.9์ ˆ "๋…ธ๋“œ ์‚ญ์ œ" ์ฐธ๊ณ )
      // removeChild ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค NodeList ๊ฐ์ฒด์ธ childNodes๊ฐ€ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค.
      // ๋”ฐ๋ผ์„œ ์ฒซ ๋ฒˆ์งธ, ์„ธ ๋ฒˆ์งธ ๋‹ค์„ฏ ๋ฒˆ์งธ ์š”์†Œ๋งŒ ์‚ญ์ œ๋œ๋‹ค.
      $fruits.removeChild(childNodes[i]);
    }

    // ์˜ˆ์ƒ๊ณผ ๋‹ค๋ฅด๊ฒŒ $fruits ์š”์†Œ์˜ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์‚ญ์ œ๋˜์ง€ ์•Š๋Š”๋‹ค.
    console.log(childNodes); // NodeList(2) [li, li]
  </script>
</html>

39-24

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li>Apple</li>
      <li>Banana</li>
    </ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // childNodes ํ”„๋กœํผํ‹ฐ๋Š” NodeList ๊ฐ์ฒด(live)๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    const { childNodes } = $fruits;

    // ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ NodeList ๊ฐ์ฒด๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
    [...childNodes].forEach(childNode => {
      $fruits.removeChild(childNode);
    });

    // $fruits ์š”์†Œ์˜ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ๊ฐ€ ๋ชจ๋‘ ์‚ญ์ œ๋˜์—ˆ๋‹ค.
    console.log(childNodes); // NodeList []
  </script>
</html>

39-25

<ul id="fruits">
  <li class="apple">Apple</li>
  <li class="banana">Banana</li>
  <li class="orange">Orange</li>
</ul>

39-26

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li class="apple">Apple</li>
      <li class="banana">Banana</li>
      <li class="orange">Orange</li>
    </ul>
  </body>
</html>

39-27

<ul id="fruits"><li
  class="apple">Apple</li><li
  class="banana">Banana</li><li
  class="orange">Orange</li></ul>

39-28

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li class="apple">Apple</li>
      <li class="banana">Banana</li>
      <li class="orange">Orange</li>
    </ul>
  </body>
  <script>
    // ๋…ธ๋“œ ํƒ์ƒ‰์˜ ๊ธฐ์ ์ด ๋˜๋Š” #fruits ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ทจ๋“ํ•œ๋‹ค.
    const $fruits = document.getElementById('fruits');

    // #fruits ์š”์†Œ์˜ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    // childNodes ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋ฐ˜ํ™˜ํ•œ NodeList์—๋Š” ์š”์†Œ ๋…ธ๋“œ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ…์ŠคํŠธ ๋…ธ๋“œ๋„ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.
    console.log($fruits.childNodes);
    // NodeList(7) [text, li.apple, text, li.banana, text, li.orange, text]

    // #fruits ์š”์†Œ์˜ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    // children ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋ฐ˜ํ™˜ํ•œ HTMLCollection์—๋Š” ์š”์†Œ ๋…ธ๋“œ๋งŒ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.
    console.log($fruits.children);
    // HTMLCollection(3) [li.apple, li.banana, li.orange]

    // #fruits ์š”์†Œ์˜ ์ฒซ ๋ฒˆ์งธ ์ž์‹ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    // firstChild ํ”„๋กœํผํ‹ฐ๋Š” ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
    console.log($fruits.firstChild); // #text

    // #fruits ์š”์†Œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    // lastChild ํ”„๋กœํผํ‹ฐ๋Š” ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
    console.log($fruits.lastChild); // #text

    // #fruits ์š”์†Œ์˜ ์ฒซ ๋ฒˆ์งธ ์ž์‹ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    // firstElementChild ํ”„๋กœํผํ‹ฐ๋Š” ์š”์†Œ ๋…ธ๋“œ๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    console.log($fruits.firstElementChild); // li.apple

    // #fruits ์š”์†Œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    // lastElementChild ํ”„๋กœํผํ‹ฐ๋Š” ์š”์†Œ ๋…ธ๋“œ๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    console.log($fruits.lastElementChild); // li.orange
  </script>
</html>

39-29

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
    </ul>
  </body>
  <script>
    // ๋…ธ๋“œ ํƒ์ƒ‰์˜ ๊ธฐ์ ์ด ๋˜๋Š” #fruits ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ทจ๋“ํ•œ๋‹ค.
    const $fruits = document.getElementById('fruits');

    // #fruits ์š”์†Œ์— ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
    // hasChildNodes ๋ฉ”์„œ๋“œ๋Š” ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ํฌํ•จํ•˜์—ฌ ์ž์‹ ๋…ธ๋“œ์˜ ์กด์žฌ๋ฅผ ํ™•์ธํ•œ๋‹ค.
    console.log($fruits.hasChildNodes()); // true
  </script>
</html>

39-30

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
    </ul>
  </body>
  <script>
    // ๋…ธ๋“œ ํƒ์ƒ‰์˜ ๊ธฐ์ ์ด ๋˜๋Š” #fruits ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ทจ๋“ํ•œ๋‹ค.
    const $fruits = document.getElementById('fruits');

    // hasChildNodes ๋ฉ”์„œ๋“œ๋Š” ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ํฌํ•จํ•˜์—ฌ ์ž์‹ ๋…ธ๋“œ์˜ ์กด์žฌ๋ฅผ ํ™•์ธํ•œ๋‹ค.
    console.log($fruits.hasChildNodes()); // true

    // ์ž์‹ ๋…ธ๋“œ ์ค‘์— ํ…์ŠคํŠธ ๋…ธ๋“œ๊ฐ€ ์•„๋‹Œ ์š”์†Œ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€๋Š” ํ™•์ธํ•œ๋‹ค.
    console.log(!!$fruits.children.length); // 0 -> false
    // ์ž์‹ ๋…ธ๋“œ ์ค‘์— ํ…์ŠคํŠธ ๋…ธ๋“œ๊ฐ€ ์•„๋‹Œ ์š”์†Œ ๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€๋Š” ํ™•์ธํ•œ๋‹ค.
    console.log(!!$fruits.childElementCount); // 0 -> false
  </script>
</html>

39-31

<!DOCTYPE html>
<html>
<body>
  <div id="foo">Hello</div>
  <script>
    // ์š”์†Œ ๋…ธ๋“œ์˜ ํ…์ŠคํŠธ ๋…ธ๋“œ๋Š” firstChild ํ”„๋กœํผํ‹ฐ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
    console.log(document.getElementById('foo').firstChild); // #text
  </script>
</body>
</html>

39-32

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li class="apple">Apple</li>
      <li class="banana">Banana</li>
      <li class="orange">Orange</li>
    </ul>
  </body>
  <script>
    // ๋…ธ๋“œ ํƒ์ƒ‰์˜ ๊ธฐ์ ์ด ๋˜๋Š” .banana ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ทจ๋“ํ•œ๋‹ค.
    const $banana = document.querySelector('.banana');

    // .banana ์š”์†Œ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    console.log($banana.parentNode); // ul#fruits
  </script>
</html>

39-33

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li class="apple">Apple</li>
      <li class="banana">Banana</li>
      <li class="orange">Orange</li>
    </ul>
  </body>
  <script>
    // ๋…ธ๋“œ ํƒ์ƒ‰์˜ ๊ธฐ์ ์ด ๋˜๋Š” #fruits ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ทจ๋“ํ•œ๋‹ค.
    const $fruits = document.getElementById('fruits');

    // #fruits ์š”์†Œ์˜ ์ฒซ ๋ฒˆ์งธ ์ž์‹ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    // firstChild ํ”„๋กœํผํ‹ฐ๋Š” ์š”์†Œ ๋…ธ๋“œ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
    const { firstChild } = $fruits;
    console.log(firstChild); // #text

    // #fruits ์š”์†Œ์˜ ์ฒซ ๋ฒˆ์งธ ์ž์‹ ๋…ธ๋“œ(ํ…์ŠคํŠธ ๋…ธ๋“œ)์˜ ๋‹ค์Œ ํ˜•์ œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    // nextSibling ํ”„๋กœํผํ‹ฐ๋Š” ์š”์†Œ ๋…ธ๋“œ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
    const { nextSibling } = firstChild;
    console.log(nextSibling); // li.apple

    // li.apple ์š”์†Œ์˜ ์ด์ „ ํ˜•์ œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    // previousSibling ํ”„๋กœํผํ‹ฐ๋Š” ์š”์†Œ ๋…ธ๋“œ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
    const { previousSibling } = nextSibling;
    console.log(previousSibling); // #text

    // #fruits ์š”์†Œ์˜ ์ฒซ ๋ฒˆ์งธ ์ž์‹ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    // firstElementChild ํ”„๋กœํผํ‹ฐ๋Š” ์š”์†Œ ๋…ธ๋“œ๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    const { firstElementChild } = $fruits;
    console.log(firstElementChild); // li.apple

    // #fruits ์š”์†Œ์˜ ์ฒซ ๋ฒˆ์งธ ์ž์‹ ์š”์†Œ ๋…ธ๋“œ(li.apple)์˜ ๋‹ค์Œ ํ˜•์ œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    // nextElementSibling ํ”„๋กœํผํ‹ฐ๋Š” ์š”์†Œ ๋…ธ๋“œ๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    const { nextElementSibling } = firstElementChild;
    console.log(nextElementSibling); // li.banana

    // li.banana ์š”์†Œ์˜ ์ด์ „ ํ˜•์ œ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.
    // previousElementSibling ํ”„๋กœํผํ‹ฐ๋Š” ์š”์†Œ ๋…ธ๋“œ๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    const { previousElementSibling } = nextElementSibling;
    console.log(previousElementSibling); // li.apple
  </script>
</html>

39-34

<!DOCTYPE html>
<html>
  <body>
    <div id="foo">Hello</div>
  </body>
  <script>
    // ๋ฌธ์„œ ๋…ธ๋“œ์˜ ๋…ธ๋“œ ์ •๋ณด๋ฅผ ์ทจ๋“ํ•œ๋‹ค.
    console.log(document.nodeType); // 9
    console.log(document.nodeName); // #document

    // ์š”์†Œ ๋…ธ๋“œ์˜ ๋…ธ๋“œ ์ •๋ณด๋ฅผ ์ทจ๋“ํ•œ๋‹ค.
    const $foo = document.getElementById('foo');
    console.log($foo.nodeType); // 1
    console.log($foo.nodeName); // DIV

    // ํ…์ŠคํŠธ ๋…ธ๋“œ์˜ ๋…ธ๋“œ ์ •๋ณด๋ฅผ ์ทจ๋“ํ•œ๋‹ค.
    const $textNode = $foo.firstChild;
    console.log($textNode.nodeType); // 3
    console.log($textNode.nodeName); // #text
</script>
</html>

39-35

<!DOCTYPE html>
<html>
  <body>
    <div id="foo">Hello</div>
  </body>
  <script>
    // ๋ฌธ์„œ ๋…ธ๋“œ์˜ nodeValue ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.
    console.log(document.nodeValue); // null

    // ์š”์†Œ ๋…ธ๋“œ์˜ nodeValue ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.
    const $foo = document.getElementById('foo');
    console.log($foo.nodeValue); // null

    // ํ…์ŠคํŠธ ๋…ธ๋“œ์˜ nodeValue ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.
    const $textNode = $foo.firstChild;
    console.log($textNode.nodeValue); // Hello
  </script>
</html>

39-36

<!DOCTYPE html>
<html>
  <body>
    <div id="foo">Hello</div>
  </body>
  <script>
    // 1. #foo ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ์ธ ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ์ทจ๋“ํ•œ๋‹ค.
    const $textNode = document.getElementById('foo').firstChild;

    // 2. nodeValue ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ๋…ธ๋“œ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
    $textNode.nodeValue = 'World';

    console.log($textNode.nodeValue); // World
  </script>
</html>

39-37

<!DOCTYPE html>
<html>
  <body>
    <div id="foo">Hello <span>world!</span></div>
  </body>
  <script>
    // #foo ์š”์†Œ ๋…ธ๋“œ์˜ ํ…์ŠคํŠธ๋ฅผ ๋ชจ๋‘ ์ทจ๋“ํ•œ๋‹ค. ์ด๋•Œ HTML ๋งˆํฌ์—…์€ ๋ฌด์‹œ๋œ๋‹ค.
    console.log(document.getElementById('foo').textContent); // Hello world!
  </script>
</html>

39-38

<!DOCTYPE html>
<html>
  <body>
    <div id="foo">Hello <span>world!</span></div>
  </body>
  <script>
    // #foo ์š”์†Œ ๋…ธ๋“œ๋Š” ํ…์ŠคํŠธ ๋…ธ๋“œ๊ฐ€ ์•„๋‹ˆ๋‹ค.
    console.log(document.getElementById('foo').nodeValue); // null
    // #foo ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ์ธ ํ…์ŠคํŠธ ๋…ธ๋“œ์˜ ๊ฐ’์„ ์ทจ๋“ํ•œ๋‹ค.
    console.log(document.getElementById('foo').firstChild.nodeValue); // Hello
    // span ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ์ธ ํ…์ŠคํŠธ ๋…ธ๋“œ์˜ ๊ฐ’์„ ์ทจ๋“ํ•œ๋‹ค.
    console.log(document.getElementById('foo').lastChild.firstChild.nodeValue); // world!
  </script>
</html>

39-39

<!DOCTYPE html>
<html>
  <body>
    <!-- ์š”์†Œ ๋…ธ๋“œ์˜ ์ฝ˜ํ…์ธ  ์˜์—ญ์— ๋‹ค๋ฅธ ์š”์†Œ ๋…ธ๋“œ๊ฐ€ ์—†๊ณ  ํ…์ŠคํŠธ๋งŒ ์กด์žฌ -->
    <div id="foo">Hello</div>
  </body>
  <script>
    const $foo = document.getElementById('foo');

    // ์š”์†Œ ๋…ธ๋“œ์˜ ์ฝ˜ํ…์ธ  ์˜์—ญ์— ์ž์‹ ์š”์†Œ ๋…ธ๋“œ๊ฐ€ ์—†๊ณ  ํ…์ŠคํŠธ๋งŒ ์กด์žฌํ•œ๋‹ค๋ฉด
    // firstChild.nodeValue์™€ textContent๋Š” ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    console.log($foo.textContent === $foo.firstChild.nodeValue); // true
  </script>
</html>

39-40

<!DOCTYPE html>
<html>
  <body>
    <div id="foo">Hello <span>world!</span></div>
  </body>
  <script>
    // #foo ์š”์†Œ ๋…ธ๋“œ์˜ ๋ชจ๋“  ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์ œ๊ฑฐ๋˜๊ณ  ํ• ๋‹นํ•œ ๋ฌธ์ž์—ด์ด ํ…์ŠคํŠธ๋กœ ์ถ”๊ฐ€๋œ๋‹ค.
    // ์ด๋•Œ HTML ๋งˆํฌ์—…์ด ํŒŒ์‹ฑ๋˜์ง€ ์•Š๋Š”๋‹ค.
    document.getElementById('foo').textContent = 'Hi <span>there!</span>';
  </script>
</html>

39-41

<!DOCTYPE html>
<html>
  <body>
    <div id="foo">Hello <span>world!</span></div>
  </body>
  <script>
    // #foo ์š”์†Œ์˜ ์ฝ˜ํ…์ธ  ์˜์—ญ ๋‚ด์˜ HTML ๋งˆํฌ์—…์„ ๋ฌธ์ž์—ด๋กœ ์ทจ๋“ํ•œ๋‹ค.
    console.log(document.getElementById('foo').innerHTML);
    // "Hello <span>world!</span>"
  </script>
</html>

39-42

<!DOCTYPE html>
<html>
  <body>
    <div id="foo">Hello <span>world!</span></div>
  </body>
  <script>
    // HTML ๋งˆํฌ์—…์ด ํŒŒ์‹ฑ๋˜์–ด ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋กœ DOM์— ๋ฐ˜์˜๋œ๋‹ค.
    document.getElementById('foo').innerHTML = 'Hi <span>there!</span>';
  </script>
</html>

39-43

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li class="apple">Apple</li>
    </ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // ๋…ธ๋“œ ์ถ”๊ฐ€
    $fruits.innerHTML += '<li class="banana">Banana</li>';

    // ๋…ธ๋“œ ๊ต์ฒด
    $fruits.innerHTML = '<li class="orange">Orange</li>';

    // ๋…ธ๋“œ ์‚ญ์ œ
    $fruits.innerHTML = '';
  </script>
</html>

39-44

<!DOCTYPE html>
<html>
  <body>
    <div id="foo">Hello</div>
  </body>
  <script>
    // innerHTML ํ”„๋กœํผํ‹ฐ๋กœ ์Šคํฌ๋ฆฝํŠธ ํƒœ๊ทธ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ํ•œ๋‹ค.
    // HTML5๋Š” innerHTML ํ”„๋กœํผํ‹ฐ๋กœ ์‚ฝ์ž…๋œ script ์š”์†Œ ๋‚ด์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.
    document.getElementById('foo').innerHTML
      = '<script>alert(document.cookie)</script>';
  </script>
</html>

39-45

<!DOCTYPE html>
<html>
  <body>
    <div id="foo">Hello</div>
  </body>
  <script>
    // ์—๋Ÿฌ ์ด๋ฒคํŠธ๋ฅผ ๊ฐ•์ œ๋กœ ๋ฐœ์ƒ์‹œ์ผœ์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ํ•œ๋‹ค.
    document.getElementById('foo').innerHTML
      = `<img src="x" onerror="alert(document.cookie)">`;
  </script>
</html>

39-46

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li class="apple">Apple</li>
    </ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // ๋…ธ๋“œ ์ถ”๊ฐ€
    $fruits.innerHTML += '<li class="banana">Banana</li>';
  </script>
</html>

39-47

$fruits.innerHTML += '<li class="banana">Banana</li>';

39-48

$fruits.innerHTML = $fruits.innerHTML + '<li class="banana">Banana</li>';
// '<li class="apple">Apple</li>' + '<li class="banana">Banana</li>'

39-49

<ul id="fruits">
  <li class="apple">Apple</li>
  <li class="orange">Orange</li>
</ul>

39-50

<!DOCTYPE html>
<html>
  <body>
    <!-- beforebegin -->
    <div id="foo">
      <!-- afterbegin -->
      text
      <!-- beforeend -->
    </div>
    <!-- afterend -->
  </body>
  <script>
    const $foo = document.getElementById('foo');

    $foo.insertAdjacentHTML('beforebegin', '<p>beforebegin</p>');
    $foo.insertAdjacentHTML('afterbegin', '<p>afterbegin</p>');
    $foo.insertAdjacentHTML('beforeend', '<p>beforeend</p>');
    $foo.insertAdjacentHTML('afterend', '<p>afterend</p>');
  </script>
</html>

39-51

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li>Apple</li>
    </ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // 1. ์š”์†Œ ๋…ธ๋“œ ์ƒ์„ฑ
    const $li = document.createElement('li');

    // 2. ํ…์ŠคํŠธ ๋…ธ๋“œ ์ƒ์„ฑ
    const textNode = document.createTextNode('Banana');

    // 3. ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ $li ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
    $li.appendChild(textNode);

    // 4. $li ์š”์†Œ ๋…ธ๋“œ๋ฅผ #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
    $fruits.appendChild($li);
  </script>
</html>

39-52

// 1. ์š”์†Œ ๋…ธ๋“œ ์ƒ์„ฑ
const $li = document.createElement('li');

39-53

// 1. ์š”์†Œ ๋…ธ๋“œ ์ƒ์„ฑ
const $li = document.createElement('li');
// ์ƒ์„ฑ๋œ ์š”์†Œ ๋…ธ๋“œ๋Š” ์•„๋ฌด๋Ÿฐ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†๋‹ค.
console.log($li.childNodes); // NodeList []

39-54

// 2. ํ…์ŠคํŠธ ๋…ธ๋“œ ์ƒ์„ฑ
const textNode = document.createTextNode('Banana');

39-55

// 3. ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ $li ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
$li.appendChild(textNode);

39-56

// ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
$li.appendChild(document.createTextNode('Banana'));

// $li ์š”์†Œ ๋…ธ๋“œ์— ์ž์‹ ๋…ธ๋“œ๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋Š” ์œ„ ์ฝ”๋“œ์™€ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.
$li.textContent = 'Banana';

39-57

// 4. $li ์š”์†Œ ๋…ธ๋“œ๋ฅผ #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
$fruits.appendChild($li);

39-58

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits"></ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    ['Apple', 'Banana', 'Orange'].forEach(text => {
      // 1. ์š”์†Œ ๋…ธ๋“œ ์ƒ์„ฑ
      const $li = document.createElement('li');

      // 2. ํ…์ŠคํŠธ ๋…ธ๋“œ ์ƒ์„ฑ
      const textNode = document.createTextNode(text);

      // 3. ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ $li ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
      $li.appendChild(textNode);

      // 4. $li ์š”์†Œ ๋…ธ๋“œ๋ฅผ #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
      $fruits.appendChild($li);
    });
  </script>
</html>

39-59

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits"></ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // ์ปจํ…Œ์ด๋„ˆ ์š”์†Œ ๋…ธ๋“œ ์ƒ์„ฑ
    const $container = document.createElement('div');

    ['Apple', 'Banana', 'Orange'].forEach(text => {
      // 1. ์š”์†Œ ๋…ธ๋“œ ์ƒ์„ฑ
      const $li = document.createElement('li');

      // 2. ํ…์ŠคํŠธ ๋…ธ๋“œ ์ƒ์„ฑ
      const textNode = document.createTextNode(text);

      // 3. ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ $li ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
      $li.appendChild(textNode);

      // 4. $li ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ปจํ…Œ์ด๋„ˆ ์š”์†Œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
      $container.appendChild($li);
    });

    // 5. ์ปจํ…Œ์ด๋„ˆ ์š”์†Œ ๋…ธ๋“œ๋ฅผ #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
    $fruits.appendChild($container);
  </script>
</html>

39-60

<ul id="fruits">
  <div>
    <li>apple</li>
    <li>banana</li>
    <li>orange</li>
  </div>
</ul>

39-61

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits"></ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // DocumentFragment ๋…ธ๋“œ ์ƒ์„ฑ
    const $fragment = document.createDocumentFragment();

    ['Apple', 'Banana', 'Orange'].forEach(text => {
      // 1. ์š”์†Œ ๋…ธ๋“œ ์ƒ์„ฑ
      const $li = document.createElement('li');

      // 2. ํ…์ŠคํŠธ ๋…ธ๋“œ ์ƒ์„ฑ
      const textNode = document.createTextNode(text);

      // 3. ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ $li ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
      $li.appendChild(textNode);

      // 4. $li ์š”์†Œ ๋…ธ๋“œ๋ฅผ DocumentFragment ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
      $fragment.appendChild($li);
    });

    // 5. DocumentFragment ๋…ธ๋“œ๋ฅผ #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
    $fruits.appendChild($fragment);
  </script>
</html>

39-62

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li>Apple</li>
      <li>Banana</li>
    </ul>
  </body>
  <script>
    // ์š”์†Œ ๋…ธ๋“œ ์ƒ์„ฑ
    const $li = document.createElement('li');

    // ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ $li ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
    $li.appendChild(document.createTextNode('Orange'));

    // $li ์š”์†Œ ๋…ธ๋“œ๋ฅผ #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
    document.getElementById('fruits').appendChild($li);
  </script>
</html>

39-63

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li>Apple</li>
      <li>Banana</li>
    </ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // ์š”์†Œ ๋…ธ๋“œ ์ƒ์„ฑ
    const $li = document.createElement('li');

    // ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ $li ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
    $li.appendChild(document.createTextNode('Orange'));

    // $li ์š”์†Œ ๋…ธ๋“œ๋ฅผ #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ์š”์†Œ ์•ž์— ์‚ฝ์ž…
    $fruits.insertBefore($li, $fruits.lastElementChild);
    // Apple - Orange - Banana
  </script>
</html>

39-64

<!DOCTYPE html>
<html>
  <body>
    <div>test</div>
    <ul id="fruits">
      <li>Apple</li>
      <li>Banana</li>
    </ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // ์š”์†Œ ๋…ธ๋“œ ์ƒ์„ฑ
    const $li = document.createElement('li');

    // ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ $li ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
    $li.appendChild(document.createTextNode('Orange'));

    // ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋…ธ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ์ด์–ด์•ผ ํ•œ๋‹ค.
    $fruits.insertBefore($li, document.querySelector('div'));
    // DOMException
  </script>
</html>

39-65

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li>Apple</li>
      <li>Banana</li>
    </ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // ์š”์†Œ ๋…ธ๋“œ ์ƒ์„ฑ
    const $li = document.createElement('li');

    // ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ $li ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
    $li.appendChild(document.createTextNode('Orange'));

    // ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋…ธ๋“œ๊ฐ€ null์ด๋ฉด $li ์š”์†Œ ๋…ธ๋“œ๋ฅผ #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
    $fruits.insertBefore($li, null);
  </script>
</html>

39-66

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li>Apple</li>
      <li>Banana</li>
      <li>Orange</li>
    </ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ทจ๋“
    const [$apple, $banana, ] = $fruits.children;

    // ์ด๋ฏธ ์กด์žฌํ•˜๋Š” $apple ์š”์†Œ ๋…ธ๋“œ๋ฅผ #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ์ด๋™
    $fruits.appendChild($apple); // Banana - Orange - Apple

    // ์ด๋ฏธ ์กด์žฌํ•˜๋Š” $banana ์š”์†Œ ๋…ธ๋“œ๋ฅผ #fruits ์š”์†Œ์˜ ๋งˆ์ง€๋ง‰ ์ž์‹ ๋…ธ๋“œ ์•ž์œผ๋กœ ์ด๋™
    $fruits.insertBefore($banana, $fruits.lastElementChild);
    // Orange - Banana - Apple
  </script>
</html>

39-67

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li>Apple</li>
    </ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');
    const $apple = $fruits.firstElementChild;

    // $apple ์š”์†Œ๋ฅผ ์–•์€ ๋ณต์‚ฌํ•˜์—ฌ ์‚ฌ๋ณธ์„ ์ƒ์„ฑ. ํ…์ŠคํŠธ ๋…ธ๋“œ๊ฐ€ ์—†๋Š” ์‚ฌ๋ณธ์ด ์ƒ์„ฑ๋œ๋‹ค.
    const $shallowClone = $apple.cloneNode();
    // ์‚ฌ๋ณธ ์š”์†Œ ๋…ธ๋“œ์— ํ…์ŠคํŠธ ์ถ”๊ฐ€
    $shallowClone.textContent = 'Banana';
    // ์‚ฌ๋ณธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
    $fruits.appendChild($shallowClone);

    // #fruits ์š”์†Œ๋ฅผ ๊นŠ์€ ๋ณต์‚ฌํ•˜์—ฌ ๋ชจ๋“  ์ž์† ๋…ธ๋“œ๊ฐ€ ํฌํ•จ๋œ ์‚ฌ๋ณธ์„ ์ƒ์„ฑ
    const $deepClone = $fruits.cloneNode(true);
    // ์‚ฌ๋ณธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€
    $fruits.appendChild($deepClone);
  </script>
</html>

39-68

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li>Apple</li>
    </ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // ๊ธฐ์กด ๋…ธ๋“œ์™€ ๊ต์ฒดํ•  ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑ
    const $newChild = document.createElement('li');
    $newChild.textContent = 'Banana';

    // #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ์ฒซ ๋ฒˆ์งธ ์ž์‹ ์š”์†Œ ๋…ธ๋“œ๋ฅผ $newChild ์š”์†Œ ๋…ธ๋“œ๋กœ ๊ต์ฒด
    $fruits.replaceChild($newChild, $fruits.firstElementChild);
  </script>
</html>

39-69

<!DOCTYPE html>
<html>
  <body>
    <ul id="fruits">
      <li>Apple</li>
      <li>Banana</li>
    </ul>
  </body>
  <script>
    const $fruits = document.getElementById('fruits');

    // #fruits ์š”์†Œ ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ DOM์—์„œ ์‚ญ์ œ
    $fruits.removeChild($fruits.lastElementChild);
  </script>
</html>

39-70

<input id="user" type="text" value="ungmo2">

39-71

<!DOCTYPE html>
<html>
<body>
  <input id="user" type="text" value="ungmo2">
  <script>
    // ์š”์†Œ ๋…ธ๋“œ์˜ attribute ํ”„๋กœํผํ‹ฐ๋Š” ์š”์†Œ ๋…ธ๋“œ์˜ ๋ชจ๋“  ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๋…ธ๋“œ์˜ ์ฐธ์กฐ๊ฐ€ ๋‹ด๊ธด NamedNodeMap ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    const { attributes } = document.getElementById('user');
    console.log(attributes);
    // NamedNodeMap {0: id, 1: type, 2: value, id: id, type: type, value: value, length: 3}

    // ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’ ์ทจ๋“
    console.log(attributes.id.value); // user
    console.log(attributes.type.value); // text
    console.log(attributes.value.value); // ungmo2
  </script>
</body>
</html>

39-72

<!DOCTYPE html>
<html>
<body>
  <input id="user" type="text" value="ungmo2">
  <script>
    const $input = document.getElementById('user');

    // value ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’์„ ์ทจ๋“
    const inputValue = $input.getAttribute('value');
    console.log(inputValue); // ungmo2

    // value ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’์„ ๋ณ€๊ฒฝ
    $input.setAttribute('value', 'foo');
    console.log($input.getAttribute('value')); // foo
  </script>
</body>
</html>

39-73

<!DOCTYPE html>
<html>
<body>
  <input id="user" type="text" value="ungmo2">
  <script>
    const $input = document.getElementById('user');

    // value ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์˜ ์กด์žฌ ํ™•์ธ
    if ($input.hasAttribute('value')) {
      // value ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์‚ญ์ œ
      $input.removeAttribute('value');
    }

    // value ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๊ฐ€ ์‚ญ์ œ๋˜์—ˆ๋‹ค.
    console.log($input.hasAttribute('value')); // false
  </script>
</body>
</html>

39-74

<!DOCTYPE html>
<html>
<body>
  <input id="user" type="text" value="ungmo2">
  <script>
    const $input = document.getElementById('user');

    // ์š”์†Œ ๋…ธ๋“œ์˜ value ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ณ€๊ฒฝ
    $input.value = 'foo';

    // ์š”์†Œ ๋…ธ๋“œ์˜ value ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ์ฐธ์กฐ
    console.log($input.value); // foo
  </script>
</body>
</html>

39-75

<!DOCTYPE html>
<html>
<body>
  <input id="user" type="text" value="ungmo2">
  <script>
    const $input = document.getElementById('user');

    // attributes ํ”„๋กœํผํ‹ฐ์— ์ €์žฅ๋œ value ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’
    console.log($input.getAttribute('value')); // ungmo2

    // ์š”์†Œ ๋…ธ๋“œ์˜ value ํ”„๋กœํผํ‹ฐ์— ์ €์žฅ๋œ value ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’
    console.log($input.value); // ungmo2
  </script>
</body>
</html>

39-76

// attributes ํ”„๋กœํผํ‹ฐ์— ์ €์žฅ๋œ value ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’์„ ์ทจ๋“ํ•œ๋‹ค. ๊ฒฐ๊ณผ๋Š” ์–ธ์ œ๋‚˜ ๋™์ผํ•˜๋‹ค.
document.getElementById('user').getAttribute('value'); // ungmo2

39-77

<!DOCTYPE html>
<html>
<body>
  <input id="user" type="text" value="ungmo2">
  <script>
    // HTML ์š”์†Œ์— ์ง€์ •ํ•œ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’, ์ฆ‰ ์ดˆ๊ธฐ ์ƒํƒœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
    document.getElementById('user').setAttribute('value', 'foo');
  </script>
</body>
</html>

39-78

<!DOCTYPE html>
<html>
<body>
  <input id="user" type="text" value="ungmo2">
  <script>
    const $input = document.getElementById('user');

    // ์‚ฌ์šฉ์ž๊ฐ€ input ์š”์†Œ์˜ ์ž…๋ ฅ ํ•„๋“œ์— ๊ฐ’์„ ์ž…๋ ฅํ•  ๋•Œ๋งˆ๋‹ค input ์š”์†Œ ๋…ธ๋“œ์˜
    // value ํ”„๋กœํผํ‹ฐ ๊ฐ’, ์ฆ‰ ์ตœ์‹  ์ƒํƒœ ๊ฐ’์„ ์ทจ๋“ํ•œ๋‹ค. value ํ”„๋กœํผํ‹ฐ ๊ฐ’์€ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์—
    // ์˜ํ•ด ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค.
    $input.oninput = () => {
      console.log('value ํ”„๋กœํผํ‹ฐ ๊ฐ’', $input.value);
    };

    // getAttribute ๋ฉ”์„œ๋“œ๋กœ ์ทจ๋“ํ•œ HTML ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’, ์ฆ‰ ์ดˆ๊ธฐ ์ƒํƒœ ๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š๊ณ  ์œ ์ง€๋œ๋‹ค.
    console.log('value ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’', $input.getAttribute('value'));
  </script>
</body>
</html>

39-79

<!DOCTYPE html>
<html>
<body>
  <input id="user" type="text" value="ungmo2">
  <script>
    const $input = document.getElementById('user');

    // DOM ํ”„๋กœํผํ‹ฐ์— ๊ฐ’์„ ํ• ๋‹นํ•˜์—ฌ HTML ์š”์†Œ์˜ ์ตœ์‹  ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค.
    $input.value = 'foo';
    console.log($input.value); // foo

    // getAttribute ๋ฉ”์„œ๋“œ๋กœ ์ทจ๋“ํ•œ HTML ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’, ์ฆ‰ ์ดˆ๊ธฐ ์ƒํƒœ ๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š๊ณ  ์œ ์ง€๋œ๋‹ค.
    console.log($input.getAttribute('value')); // ungmo2
  </script>
</body>
</html>

39-80

<!DOCTYPE html>
<html>
<body>
  <input id="user" type="text" value="ungmo2">
  <script>
    const $input = document.getElementById('user');

    // id ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์™€ id ํ”„๋กœํผํ‹ฐ๋Š” ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ณผ ๊ด€๊ณ„์—†์ด ํ•ญ์ƒ ๋™์ผํ•œ ๊ฐ’์œผ๋กœ ์—ฐ๋™ํ•œ๋‹ค.
    $input.id = 'foo';

    console.log($input.id); // foo
    console.log($input.getAttribute('id')); // foo
  </script>
</body>
</html>

39-81

<!DOCTYPE html>
<html>
<body>
  <input type="checkbox" checked>
  <script>
    const $checkbox = document.querySelector('input[type=checkbox]');

    // getAttribute ๋ฉ”์„œ๋“œ๋กœ ์ทจ๋“ํ•œ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’์€ ์–ธ์ œ๋‚˜ ๋ฌธ์ž์—ด์ด๋‹ค.
    console.log($checkbox.getAttribute('checked')); // ''

    // DOM ํ”„๋กœํผํ‹ฐ๋กœ ์ทจ๋“ํ•œ ์ตœ์‹  ์ƒํƒœ ๊ฐ’์€ ๋ฌธ์ž์—ด์ด ์•„๋‹ ์ˆ˜๋„ ์žˆ๋‹ค.
    console.log($checkbox.checked); // true
  </script>
</body>
</html>

39-82

<!DOCTYPE html>
<html>
<body>
  <ul class="users">
    <li id="1" data-user-id="7621" data-role="admin">Lee</li>
    <li id="2" data-user-id="9524" data-role="subscriber">Kim</li>
  </ul>
</body>
</html>

39-83

<!DOCTYPE html>
<html>
<body>
  <ul class="users">
    <li id="1" data-user-id="7621" data-role="admin">Lee</li>
    <li id="2" data-user-id="9524" data-role="subscriber">Kim</li>
  </ul>
  <script>
    const users = [...document.querySelector('.users').children];

    // user-id๊ฐ€ '7621'์ธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ทจ๋“ํ•œ๋‹ค.
    const user = users.find(user => user.dataset.userId === '7621');
    // user-id๊ฐ€ '7621'์ธ ์š”์†Œ ๋…ธ๋“œ์—์„œ data-role์˜ ๊ฐ’์„ ์ทจ๋“ํ•œ๋‹ค.
    console.log(user.dataset.role); // "admin"

    // user-id๊ฐ€ '7621'์ธ ์š”์†Œ ๋…ธ๋“œ์˜ data-role ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
    user.dataset.role = 'subscriber';
    // dataset ํ”„๋กœํผํ‹ฐ๋Š” DOMStringMap ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    console.log(user.dataset); // DOMStringMap {userId: "7621", role: "subscriber"}
  </script>
</body>
</html>

39-84

<!DOCTYPE html>
<html>
<body>
  <ul class="users">
    <li id="1" data-user-id="7621">Lee</li>
    <li id="2" data-user-id="9524">Kim</li>
  </ul>
  <script>
    const users = [...document.querySelector('.users').children];

    // user-id๊ฐ€ '7621'์ธ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ทจ๋“ํ•œ๋‹ค.
    const user = users.find(user => user.dataset.userId === '7621');

    // user-id๊ฐ€ '7621'์ธ ์š”์†Œ ๋…ธ๋“œ์— ์ƒˆ๋กœ์šด data ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
    user.dataset.role = 'admin';
    console.log(user.dataset);
    /*
    DOMStringMap {userId: "7621", role: "admin"}
    -> <li id="1" data-user-id="7621" data-role="admin">Lee</li>
    */
  </script>
</body>
</html>

39-85

<!DOCTYPE html>
<html>
<body>
  <div style="color: red">Hello World</div>
  <script>
    const $div = document.querySelector('div');

    // ์ธ๋ผ์ธ ์Šคํƒ€์ผ ์ทจ๋“
    console.log($div.style); // CSSStyleDeclaration { 0: "color", ... }

    // ์ธ๋ผ์ธ ์Šคํƒ€์ผ ๋ณ€๊ฒฝ
    $div.style.color = 'blue';

    // ์ธ๋ผ์ธ ์Šคํƒ€์ผ ์ถ”๊ฐ€
    $div.style.width = '100px';
    $div.style.height = '100px';
    $div.style.backgroundColor = 'yellow';
  </script>
</body>
</html>

39-86

$div.style.backgroundColor = 'yellow';

39-87

$div.style['background-color'] = 'yellow';

39-88

$div.style.width = '100px';

39-89

<!DOCTYPE html>
<html>
<head>
  <style>
    .box {
      width: 100px; height: 100px;
      background-color: antiquewhite;
    }
    .red { color: red; }
    .blue { color: blue; }
  </style>
</head>
<body>
  <div class="box red">Hello World</div>
  <script>
    const $box = document.querySelector('.box');

    // .box ์š”์†Œ์˜ class ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’์„ ์ทจ๋“
    console.log($box.className); // 'box red'

    // .box ์š”์†Œ์˜ class ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’ ์ค‘์—์„œ 'red'๋งŒ 'blue'๋กœ ๋ณ€๊ฒฝ
    $box.className = $box.className.replace('red', 'blue');
  </script>
</body>
</html>

39-90

<!DOCTYPE html>
<html>
<head>
  <style>
    .box {
      width: 100px; height: 100px;
      background-color: antiquewhite;
    }
    .red { color: red; }
    .blue { color: blue; }
  </style>
</head>
<body>
  <div class="box red">Hello World</div>
  <script>
    const $box = document.querySelector('.box');

    // .box ์š”์†Œ์˜ class ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ์ •๋ณด๋ฅผ ๋‹ด์€ DOMTokenList ๊ฐ์ฒด๋ฅผ ์ทจ๋“
    // classList๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” DOMTokenList ๊ฐ์ฒด๋Š” HTMLCollection๊ณผ NodeList์™€ ๊ฐ™์ด
    // ๋…ธ๋“œ ๊ฐ์ฒด์˜ ์ƒํƒœ ๋ณ€ํ™”๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ˜์˜ํ•˜๋Š” ์‚ด์•„ ์žˆ๋Š”(live) ๊ฐ์ฒด๋‹ค.
    console.log($box.classList);
    // DOMTokenList(2) [length: 2, value: "box blue", 0: "box", 1: "blue"]

    // .box ์š”์†Œ์˜ class ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๊ฐ’ ์ค‘์—์„œ 'red'๋งŒ 'blue'๋กœ ๋ณ€๊ฒฝ
    $box.classList.replace('red', 'blue');
  </script>
</body>
</html>

39-91

$box.classList.add('foo'); // -> class="box red foo"
$box.classList.add('bar', 'baz'); // -> class="box red foo bar baz"

39-92

$box.classList.remove('foo'); // -> class="box red bar baz"
$box.classList.remove('bar', 'baz'); // -> class="box red"
$box.classList.remove('x'); // -> class="box red"

39-93

$box.classList.item(0); // -> "box"
$box.classList.item(1); // -> "red"

39-94

$box.classList.contains('box');  // -> true
$box.classList.contains('blue'); // -> false

39-95

$box.classList.replace('red', 'blue'); // -> class="box blue"

39-96

$box.classList.toggle('foo'); // -> class="box blue foo"
$box.classList.toggle('foo'); // -> class="box blue"

39-97

// class ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์— ๊ฐ•์ œ๋กœ 'foo' ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€
$box.classList.toggle('foo', true); // -> class="box blue foo"
// class ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์—์„œ ๊ฐ•์ œ๋กœ 'foo' ํด๋ž˜์Šค๋ฅผ ์ œ๊ฑฐ
$box.classList.toggle('foo', false); // -> class="box blue"

39-98

<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      color: red;
    }
    .box {
      width: 100px;
      height: 50px;
      background-color: cornsilk;
      border: 1px solid black;
    }
  </style>
</head>
<body>
  <div class="box">Box</div>
  <script>
    const $box = document.querySelector('.box');

    // .box ์š”์†Œ์— ์ ์šฉ๋œ ๋ชจ๋“  CSS ์Šคํƒ€์ผ์„ ๋‹ด๊ณ  ์žˆ๋Š” CSSStyleDeclaration ๊ฐ์ฒด๋ฅผ ์ทจ๋“
    const computedStyle = window.getComputedStyle($box);
    console.log(computedStyle); // CSSStyleDeclaration

    // ์ž„๋ฒ ๋”ฉ ์Šคํƒ€์ผ
    console.log(computedStyle.width); // 100px
    console.log(computedStyle.height); // 50px
    console.log(computedStyle.backgroundColor); // rgb(255, 248, 220)
    console.log(computedStyle.border); // 1px solid rgb(0, 0, 0)

    // ์ƒ์† ์Šคํƒ€์ผ(body -> .box)
    console.log(computedStyle.color); // rgb(255, 0, 0)

    // ๊ธฐ๋ณธ ์Šคํƒ€์ผ
    console.log(computedStyle.display); // block
  </script>
</body>
</html>

39-99

<!DOCTYPE html>
<html>
<head>
  <style>
    .box:before {
      content: 'Hello';
    }
  </style>
</head>
<body>
  <div class="box">Box</div>
  <script>
    const $box = document.querySelector('.box');

    // ์˜์‚ฌ ์š”์†Œ :before์˜ ์Šคํƒ€์ผ์„ ์ทจ๋“ํ•œ๋‹ค.
    const computedStyle = window.getComputedStyle($box, ':before');
    console.log(computedStyle.content); // "Hello"
  </script>
</body>
</html>