Javascript/Javascript

[๊ธฐ์ดˆ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] DOM(์•„์ง ์ž‘์„ฑ์ค‘)

Rainbow๐ŸŒˆCoder 2022. 3. 21. 14:50
728x90

https://youtu.be/Y3_2BLb3hz8

* ํ•ด๋‹น ํฌ์ŠคํŒ…์€ 1๋ถ„ ์ฝ”๋”ฉ๋‹˜์˜ ์œ ํŠœ๋ธŒ ๋ฌด๋ฃŒ ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ํ•™์Šต, ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค*

part1,2 ๋‚ด์šฉ

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <script>
      //์ƒ์„ฑ์ž(constructor)
      function Person(username, age) {
        this.username = username;
        this.age = age;
        this.introduce = function () {
          console.log(
            "์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ " +
              this.username +
              "์ด๊ณ , ์ œ ๋‚˜์ด๋Š” " +
              this.age +
              "์ž…๋‹ˆ๋‹ค"
          );
        };
      }

      //์ธ์Šคํ„ด์Šค(instance)
      const ์ฒ ์ˆ˜ = new Person("์ฒ ์ˆ˜", 6);
      const ์งฑ๊ตฌ = new Person("์งฑ๊ตฌ", 6);

      ์ฒ ์ˆ˜.introduce();
      ์งฑ๊ตฌ.introduce();
    </script>
  </body>
</html>

DOM๊ณผ Event๊นŒ์ง€ ํ•˜๋ฉด ๋™์ ์ธ ํŽ˜์ด์ง€๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค!

์›น HTML ํƒœ๊ทธ๋“ค์„ ์งœ๋‹ค๋ณด๋ฉด ํŠธ๋ฆฌ๊ตฌ์กฐ๊ฐ€ ํ˜•์„ฑ์ด ๋˜๊ณ ,

์ด ํŠธ๋ฆฌ๊ตฌ์กฐ๋ฅผ ์ด๋ฃจ๋Š” ์š”์†Œ ๊ฐ๊ฐ ํ•˜๋‚˜ํ•˜๋‚˜๊ฐ€ ์ „๋ถ€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด์ด๋‹ค.

(ํƒœ๊ทธ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ…์ŠคํŠธ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€ ์ทจ๊ธ‰... ์‹ฌ์ง€์–ด ํด๋ž˜์Šค๋‚˜ ID, ๋˜๋Š” ํ•˜์ดํผ๋งํฌ๋‚˜ img src ๊ฐ™์€ ์†์„ฑ ๊ทธ๋Ÿฐ ์• ๋“ค๋„.. DOM ๊ฐ์ฒด์— ์†ํ•œ๋‹ค ๊ฒฐ๊ตญ DOM ์Šคํฌ๋ฆฝํŠธ๋Š” html element๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์กฐ์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.)

 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
    <script>
      const title = document.getElementById("main-title");
      console.log(title);
    </script>
  </head>
  <body>
    <h1 id="main-title">DOM(Document Object Model)</h1>
    <p>DOM์ด๋ž€, ๋ฌธ์„œ์— ์žˆ๋Š” ๊ฐ์ฒด๋“ค์„ ๋ฐ”๋ผ๋ณด๋Š” ๋ชจ๋ธ</p>
    <article>
      <header>header</header>
      <section>
        <header>s header</header>
        section 1
      </section>
      <section>
        <header>s header</header>
        section 2
      </section>
    </article>
  </body>
</html>

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด ์ฝ˜์†”์— title์€ null์ด ๋  ๊ฒƒ์ด๋‹ค. ์™œ๋ƒํ•˜๋ฉด html ์š”์†Œ๋“ค์„ ์•„์ง ์ฝ์ง€๋„ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.(์œ„์—์„œ ์•„๋ž˜๋กœ ๋ฒ•์น™์— ์˜๊ฑฐํ•˜์—ฌ)

 

 

 

 

 

ํŠน์ง•

1. head ์•ˆ์— DOM์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋Š” html ๋ฌธ์„œ๊ฐ€ ๋‹ค ์ฝํž ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ์‹คํ–‰ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ window.onload = function(){} ์˜ ๋„์›€์„ ๋ฐ›์•„์•ผ ํ•œ๋‹ค!

- window๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ „์—ญ ๊ฐ์ฒด

- onload๋Š” ์ด๋ฒคํŠธ(์ข€.. ๊ตฌ์‹.. ์˜›๋‚  ์Šคํƒ€์ผ^^;)

- window.onload = function(){์•„๋ฌด ํ•จ์ˆ˜}๋œป : "์ „์ฒด ๋ฌธ์„œ๊ฐ€ ๋กœ๋“œ๊ฐ€ ๋˜๋ฉด ๊ด„ํ˜ธ์•ˆ์— ๋“ค์–ด๊ฐ„ ์•„๋ฌด ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์ฃผ์„ธ์š”."

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
    <script>
      window.onload = function () {
        const title = document.getElementById("main-title");
        console.log(title);
      };
    </script>
  </head>
  <body>
    <h1 id="main-title">DOM(Document Object Model)</h1>
    <p>DOM์ด๋ž€, ๋ฌธ์„œ์— ์žˆ๋Š” ๊ฐ์ฒด๋“ค์„ ๋ฐ”๋ผ๋ณด๋Š” ๋ชจ๋ธ</p>
    <article>
      <header>header</header>
      <section>
        <header>s header</header>
        section 1
      </section>
      <section>
        <header>s header</header>
        section 2
      </section>
    </article>
  </body>
</html>

 

 

window.onload = function(){์•„๋ฌด ํ•จ์ˆ˜} //์†์„ฑ์— ๊ฐ’ ํ• ๋‹น

๋Œ€์‹ ์— ์š”์ฆ˜ ์„œํƒ€์ผ๋กœ ์ž‘์„ฑํ•ด๋ณด์ž.

 

    <script> ์œˆ๋„์šฐ ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง„ ๋ฉ”์„œ๋“œ์ธ addEventListener์˜ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ด๋ฒคํŠธ ์ด๋ฆ„์ด ๋ฌธ์ž์—ด๋กœ ๋“ค์–ด๊ฐ€๊ณ , ๋‘๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ด๋ฒคํŠธ๊ฐ€ ์ผ์–ด๋‚  ํ•จ์ˆ˜๋ฅผ ํ†ต์จฐ๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.
      window.addEventListener("load", function () {
        const title = document.getElementById("main-title");
        console.log(title);
      });
    </script>
 

๋™์ž‘์€ ๋˜‘๊ฐ™๋‹ค~(๊ฐ์ฒด์˜ ์†์„ฑ ์ค‘์— ๊ฐ’์ด ํ•จ์ˆ˜์•ˆ ์• ๋“ค์„ ํŠน๋ณ„ํžˆ '๋ฉ”์„œ๋“œ'๋กœ ๋ถ€๋ฅด๊ธฐ๋กœ ํ–ˆ๋‹ค.)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
    <script>
      window.addEventListener("load", function () {
        const title = document.getElementById("main-title");
        console.log(title);
      });
    </script>
  </head>
  <body>
    <h1 id="main-title">DOM(Document Object Model)</h1>
    <p>DOM์ด๋ž€, ๋ฌธ์„œ์— ์žˆ๋Š” ๊ฐ์ฒด๋“ค์„ ๋ฐ”๋ผ๋ณด๋Š” ๋ชจ๋ธ</p>
    <article>
      <header>header</header>
      <section>
        <header>s header</header>
        section 1
      </section>
      <section>
        <header>s header</header>
        section 2
      </section>
    </article>
  </body>
</html>

      window.addEventListener("DOMContentLoaded", function () {
        const title = document.getElementById("main-title");
        console.log(title);
      });
์‚ฌ์‹ค ์–˜๋„ ์œ„์™€ ๊ฐ™์€ ์ฝ˜์†”์ฐฝ ์ƒํƒœ์ธ๋ฐ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.
 
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="style.css" />
    <script>
      /*window.addEventListener("load", function () {
        const title = document.getElementById("main-title");
        console.log(title);
      });*/
      window.addEventListener("DOMContentLoaded", function () {
        const title = document.getElementById("main-title");
        console.log(title);
      });
    </script>
  </head>
  <body>
    <h1 id="main-title">DOM(Document Object Model)</h1>
    <p>DOM์ด๋ž€, ๋ฌธ์„œ์— ์žˆ๋Š” ๊ฐ์ฒด๋“ค์„ ๋ฐ”๋ผ๋ณด๋Š” ๋ชจ๋ธ</p>
    <article>
      <header>header</header>
      <section>
        <header>s header</header>
        section 1
      </section>
      <section>
        <header>s header</header>
        section 2
      </section>
    </article>
  </body>
</html>

์ฐจ์ด์ ์„ ์•Œ์•„๋ณด์ž

      /*window.addEventListener("load", function () {
        const title = document.getElementById("main-title");
        console.log(title);
      });*/
      window.addEventListener("DOMContentLoaded", function () {
        const title = document.getElementById("main-title");
        console.log(title);
      });

load๋Š” ์ „์ฒด๋‚ด์šฉ์ด ๋‹ค~ ๋กœ๋“œ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค.(๋ฌธ์„œ ๋‚ด์šฉ์ธ๋‚˜ ์ด๋ฏธ์ง€๊ฐ€ ๋ฌด๊ฒ๊ณ  ๋งŽ์œผ๋ฉด ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค. src ์†์˜ ์„ธ์„ธํ•œ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ค ๋กœ๋“œ๋˜์–ด์•ผ ํ•œ๋‹ค๋Š” ๋œป.)

DOMContentLoaded๋Š” DOM์˜ Content๋งŒ ๋กœ๋“œ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค. (์ฆ‰ ์•ต๊ฐ„ํ•œ html ๊ตฌ์กฐ ํƒœ๊ทธ๋งŒ ๋‹ค ๋กœ๋“œ๋˜๋ฉด ๋˜๋‹ˆ๊นŒ ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค. src๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ๋งŒ ์ฝํžˆ๋ฉด, ์ฆ‰ src ๋งํฌ ๋ฐ์ดํ„ฐ ๋กœ๋”ฉ์„ ๊ตณ์ด ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๋Š”๋‹ค๋Š” ๋œป.)

 

 


script๋ฅผ head๊ฐ€ ์•„๋‹ˆ๋ผ body ๋‹ซ๊ธฐ ์ง์ „์— ํ•˜๋ฉด ์œ„์˜ ์ž‘์—…์ด ๊ตณ์ด ํ•„์š” ์—†์–ด์ง„๋‹ค.

์ด๋ฏธ html ์ฝ”๋“œ๋ฅผ ๋‹ค ์ฝ๊ณ  ์™”๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋‹ค๋ฆด ํ•„์š”๊ฐ€ ์—†๋Š” ๊ฒƒ.

๊ทธ๋ž˜์„œ ์‚ฌ์‹ค์ƒ ์ถ”์„ธ๊ฐ€ ์Šคํฌ๋ฆฝํŠธ ํƒœ๊ทธ๋ฅผ head๊ฐ€ ์•„๋‹ˆ๋ผ body ๋‹ซ๊ธด ์ง์ „์— ๋„ฃ๋Š” ๊ฒƒ์œผ๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค.

์œ ์ € ์ž…์žฅ์—์„œ๋Š” ํ…์ŠคํŠธ ํ•˜๋‚˜๋ผ๋„ ๋” ๋นจ๋ฆฌ ๋œจ๋Š” ๊ฒƒ์ด ๋А๋ฆฌ์ง€ ์•Š๊ฒŒ ๋А๊ปด์ง€๋‹ˆ๊นŒ...

๋ฌผ๋ก  SPA, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ํ™”๋ฉด์„ ๋‹ค ๊ทธ๋ ค๋‚ด๋Š” ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ํ•ด๋‹น๋˜๋Š” ๋‚ด์šฉ ์•„๋‹˜

 

728x90