[TIL] Day16 #DOM

Beanxxยท2022๋…„ 5์›” 17์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
16/120
post-thumbnail

[TIL] Day16
[SEB FE] Day16

โ˜‘๏ธย DOM ๊ธฐ์ดˆ

  • DOM(Document Object Model): HTML ์š”์†Œ๋ฅผ JS Object์ฒ˜๋Ÿผ ์กฐ์ž‘(Manipulation)ํ•  ์ˆ˜ ์žˆ๋Š” Model
    • HTML์˜ ์•„์ฃผ ์ž‘์€ ๋ถ€๋ถ„๊นŒ์ง€ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ
    • HTML๋กœ ๊ตฌ์„ฑ๋œ ์›น ํŽ˜์ด์ง€๋ฅผ ๋™์ ์œผ๋กœ ์›€์ง์ด๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ

โœ”๏ธ script.js

  • <script> ์š”์†Œ๋Š” ๋“ฑ์žฅ๊ณผ ํ•จ๊ป˜ ์‹คํ–‰๋จ
// HTML์— JS ์ ์šฉ
<script src="myScriptFile.js"></script>

<script> ์š”์†Œ ์ถ”๊ฐ€ ๋ฐฉ๋ฒ•

  1. <head> ์•ˆ์ชฝ์— ์‚ฝ์ž…

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <title>Document</title>
        <!-- script ์š”์†Œ ์‚ฝ์ž… ์œ„์น˜ -->
        <script src="myScriptFile.js"></script>
      </head>
      <body>
        <div id="msg">Hello JavaScript!</div>
      </body>
    </html>
    console.log("welcome JavaScript");
    
    let msgElement = document.querySelector("#msg");
    console.log(msgElement);
    
    // welcome JavaScript
    // null  <- js ์ฝ”๋“œ ๋จผ์ € ์ฝ์–ด์„œ html์ด ์™„์ „ํžˆ ๋กœ๋”ฉ๋˜๊ธฐ ์ „์— js ํŒŒ์ผ์ด ์‹คํ–‰๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ
  2. <body> ์š”์†Œ๊ฐ€ ๋๋‚˜๊ธฐ ์ „์— ์‚ฝ์ž…

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8" />
        <title>Document</title>
      </head>
      <body>
        <div id="msg">Hello JavaScript!</div>
        <!-- script ์š”์†Œ ์‚ฝ์ž… ์œ„์น˜ -->
        <script src="myScriptFile.js"></script>
      </body>
    </html>
    console.log("welcome JavaScript");
    
    let msgElement = document.querySelector("#msg");
    console.log(msgElement);
    
    // welcome JavaScript
    // <div id="msg">Hello JavaScript!</div>

  • DOM ๊ตฌ์กฐ๋ฅผ ์กฐํšŒํ•  ๋• console.dir์ด ์œ ์šฉ
    • console.dir์€ console.log์™€ ๋‹ฌ๋ฆฌ DOM์„ ๊ฐ์ฒด์˜ ๋ชจ์Šต์œผ๋กœ ์ถœ๋ ฅ

      // body์˜ ์ž์‹ ์š”์†Œ ์ฐพ๊ธฐ
      console.dir(document.body.children)
      
      // ๋”ฐ๋กœ ๋ณ€์ˆ˜ ์„ ์–ธ์„ ํ•ด์„œ ์ •๋ณด๋ฅผ ์ €์žฅํ•ด๋‘๋ฉด, ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์–ธ์ œ๋“ ์ง€ ์ ‘๊ทผ ๊ฐ€๋Šฅ
      let newsContents = document.body.children[1]
      console.dir(newsContents)
      
      // ๋ถ€๋ชจ ์š”์†Œ ์ฐพ๊ธฐ
      console.dir(newsContents.parentElement)


โ˜‘๏ธย DOM ๋‹ค๋ฃจ๊ธฐ

โœ๐Ÿป CRUD(Create, Read, Update and Delete)

1๏ธโƒฃ CREATE

: HTML Element ์ถ”๊ฐ€

document.createElement('div')

// <div></div>

// ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ์–ด๋–ค ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹น
const tweetDiv = document.createElement('div')

2๏ธโƒฃ APPEND

: ์ƒ์„ฑํ•œ HTML Element๋ฅผ ํŠธ๋ฆฌ ๊ตฌ์กฐ์™€ ์—ฐ๊ฒฐ (๋ถ€๋ชจ ๋…ธ๋“œ์— ํฌํ•จ)

document.body.append(tweetDiv)

3๏ธโƒฃ READ

: HTML Element ์กฐํšŒ

  • DOM์œผ๋กœ HTML ์—˜๋ฆฌ๋จผํŠธ์˜ ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” querySelector(์…€๋ ‰ํ„ฐ ์กฐํšŒ)์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ Selector๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ํ™•์ธ
    • ์ฃผ์š” Selector: HTML ์š”์†Œ(โ€divโ€), id(โ€#tweetListโ€), class(.tweet)

4๏ธโƒฃ querySelector

: ์š”์†Œ ์กฐํšŒ

// ํด๋ž˜์Šค ์ด๋ฆ„์ด tweet์ธ HTML ์—˜๋ฆฌ๋จผํŠธ ์ค‘ ์ฒซ ๋ฒˆ์งธ ์—˜๋ฆฌ๋จผํŠธ ์กฐํšŒ
const oneTweet = document.querySelector('.tweet')

// ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์š”์†Œ๋ฅผ ํ•œ ๋ฒˆ์— ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„ , querySelectorAll ์‚ฌ์šฉ
const tweets = document.querySelectorAll('.tweet')

// querySelector์™€ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•˜๋Š” ์˜ค๋ž˜๋œ ๋ฐฉ์‹
const getOneTweet = document.getElementById('container')

โœ‹ย ์•ž์„œ ์กฐํšŒํ•œ HTML ์š”์†Œ๋“ค์€ ๋ฐฐ์—ด โŒย 

โ†’ ์ด๋Ÿฌํ•œ ๋ฐฐ์—ด ์•„๋‹Œ ๋ฐฐ์—ด์„ ์œ ์‚ฌ ๋ฐฐ์—ด, ๋ฐฐ์—ดํ˜• ๊ฐ์ฒด ๋“ฑ์œผ๋กœ ๋ถ€๋ฆ„ โ‡’ Array-like Object

5๏ธโƒฃ UPDATE

: HTML Element ๋ณ€๊ฒฝ

  • textContent: ๋น„์–ด์žˆ๋Š” div ์—˜๋ฆฌ๋จผํŠธ์— ๋ฌธ์ž์—ด ์ž…๋ ฅ
  • classList.add: ํด๋ž˜์Šค ์ถ”๊ฐ€
const oneDiv = document.createElement('div');
console.log(oneDiv) // <div></div>

// ๋ฌธ์ž์—ด ์ž…๋ ฅ
oneDiv.textContent = 'dev';
console.log(oneDiv)  // <div>dev</div>

// ํด๋ž˜์Šค ์ถ”๊ฐ€
oneDiv.classList.add('tweet');
console.log(oneDiv)  // <div class="tweet">dev</div>

// container์˜ ์ž์‹ ์š”์†Œ๋กœ ์ถ”๊ฐ€
const container = document.querySelector('#container');
container.append(oneDiv)

6๏ธโƒฃ DELETE

: HTML Element ์‚ญ์ œ

// ์‚ญ์ œํ•˜๋ ค๋Š” ์š”์†Œ์˜ ์œ„์น˜๋ฅผ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ (remove)
const container = document.querySelector('#container');
const tweetDiv = document.createElement('div');
container.append(tweetDiv)
tweetDiv.remove()

----------

// ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ž์‹ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ (innerHTML; ๋ณด์•ˆ์ƒ ๋ฌธ์ œ ์กด์žฌ)
document.querySelector('#container').innerHTML = '';

----------

// innerHTML์˜ ๋ณด์•ˆ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์—ฌ ๋Œ€์‹  ์“ธ ์ˆ˜ ์žˆ๋Š” removeChild
// removeChild: ์ž์‹ ์š”์†Œ๋ฅผ ์ง€์ •ํ•ด์„œ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์„œ๋“œ
const container = document.querySelector('#container');

// ์ž์‹ ์š”์†Œ๊ฐ€ ๋‚จ์•„์žˆ์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€, ์ฒซ๋ฒˆ์งธ ์ž์‹ ์š”์†Œ ์‚ญ์ œ
while (container.firstChild) {
	container.removeChild(container.firstChild);
}

-----

// ์ œ๋ชฉ์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„๊นŒ์ง€ ์‚ญ์ œ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€
while (container.children.length > 1) {
  container.removeChild(container.lastChild);
}

-----

// ์ง์ ‘ ํด๋ž˜์Šค ์ด๋ฆ„์ด tweet์ธ ์š”์†Œ๋งŒ ์ฐพ์•„์„œ ์‚ญ์ œ
tweets.forEach(function(tweet){
	tweet.remove();
})

// or
for (let tweet of tweets) {
	tweet.remove()
}


โœ…ย Pair Programming - Bare Minimum Requirements

: ์ž„์˜์˜ ํšŒ์›๊ฐ€์ž… ์ฐฝ์˜ id, password ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์™„๋ฃŒํ•˜๊ธฐ
โœ๐Ÿป ์•„๋ž˜์™€ ๊ฐ™์ด ์•„์ด๋””๋Š” 4๊ธ€์ž ์ด์ƒ์ด์—ฌ์•ผ ํ•˜๋ฉฐ, ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ๊ณผ ์ผ์น˜ํ•ด์•ผ ์•ˆ๋‚ด ๋ฌธ๊ตฌ๊ฐ€ ์‚ฌ๋ผ์ง„๋‹ค.

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-05-17 แ„‹แ…ฉแ„’แ…ฎ 5 07 12 แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-05-17 แ„‹แ…ฉแ„’แ…ฎ 5 07 41

<!-- index.html -->
<!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>์œ ํšจ์„ฑ ๊ฒ€์‚ฌ</title>
  <link rel="stylesheet" href="style.css">
  <script src="validator.js"></script>
</head>

<body>
  <h1>ํšŒ์›๊ฐ€์ž…</h1>

  <fieldset>
    <label>์•„์ด๋””</label>
    <input type="text" id="username">
    <div class="success-message hide">์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์•„์ด๋””์ž…๋‹ˆ๋‹ค</div>
    <div class="failure-message hide">์•„์ด๋””๋Š” ๋„ค ๊ธ€์ž ์ด์ƒ์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค</div>
  </fieldset>

  <fieldset>
    <label>๋น„๋ฐ€๋ฒˆํ˜ธ</label>
    <input type="password" id="password">
  </fieldset>

  <fieldset>
    <label>๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ</label>
    <input type="password" id="password-retype">
    <div class="mismatch-message hide">๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค</div>
  </fieldset>

  <fieldset>
    <button>ํšŒ์›๊ฐ€์ž…</button>
  </fieldset>

  <script src="script.js"></script>
</body>

</html>
// script.js
let elInputUsername = document.querySelector("#username");
let elFailureMessage = document.querySelector(".failure-message");
let elSuccessMessage = document.querySelector(".success-message");

let elPassword = document.querySelector("#password");
let elRePassword = document.querySelector("#password-retype");
let elPasswordMessage = document.querySelector(".mismatch-message");

// ์•„์ด๋”” ์ž…๋ ฅ๋ž€์— ํ‚ค๋ณด๋“œ๊ฐ€ ๋ˆŒ๋ ธ์„ ๋•Œ ์‹คํ–‰
elInputUsername.onkeyup = function () {
  console.log(elInputUsername.value);
  // isMoreThan4Length ํ•จ์ˆ˜๊ฐ€ true์ผ ๋•Œ
  if (isMoreThan4Length(elInputUsername.value)) {
    // ์„ฑ๊ณต ๋ฉ”์‹œ์ง€๊ฐ€ ๋ณด์—ฌ์•ผ ํ•จ
    elSuccessMessage.classList.remove("hide");

    // ์‹คํŒจ ๋ฉ”์‹œ์ง€๊ฐ€ ๊ฐ€๋ ค์ ธ์•ผ ํ•จ
    elFailureMessage.classList.add("hide");
  } else {
    // isMoreThan4Length ํ•จ์ˆ˜๊ฐ€ false์ผ ๋•Œ
    // ์„ฑ๊ณต ๋ฉ”์‹œ์ง€๊ฐ€ ๊ฐ€๋ ค์ ธ์•ผ ํ•จ
    elSuccessMessage.classList.add("hide");

    // ์‹คํŒจ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ณด์—ฌ์•ผ ํ•จ
    elFailureMessage.classList.remove("hide");
  }
};

// ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ ์ž…๋ ฅ๋ž€์— ํ‚ค๋ณด๋“œ๊ฐ€ ๋ˆŒ๋ ธ์„ ๋•Œ ์‹คํ–‰
elRePassword.onkeyup = function () {
  // isMatch ํ•จ์ˆ˜๊ฐ€ true์ผ ๋•Œ
  if (isMatch(elPassword.value, elRePassword.value)) {
    // ์‹คํŒจ ๋ฉ”์‹œ์ง€๊ฐ€ ๊ฐ€๋ ค์ ธ์•ผ ํ•จ
    elPasswordMessage.classList.add("hide");
  } else {
    // isMatch ํ•จ์ˆ˜๊ฐ€ false์ผ๋•Œ
    // ์‹คํŒจ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ณด์—ฌ์•ผ ํ•จ
    elPasswordMessage.classList.remove("hide");
  }
};

// 1. ์•„์ด๋”” ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ: ์•„์ด๋””๊ฐ€ 4๊ธ€์ž ์ด์ƒ์ด์—ฌ์•ผ ํ•จ
function isMoreThan4Length(value) {
  return value.length >= 4;
}

// 2. ๋น„๋ฐ€๋ฒˆํ˜ธ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ: ์ฒ˜์Œ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋‚ด์šฉ๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ๋ž€์˜ ๋‚ด์šฉ์ด ๊ฐ™์•„์•ผ ํ•จ
function isMatch(password1, password2) {
  if (password1 === password2) {
    return true;
  } else {
    return false;
  }
}
/* style.css */

body {
  background-color: lightgoldenrodyellow;
}

.hide {
  display: none;
}
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-05-17 แ„‹แ…ฉแ„’แ…ฎ 6 36 48
profile
FE developer

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