DOM ๐Ÿ’ฏ๏ธ

jungeundelilahLEEยท2020๋…„ 12์›” 15์ผ
0

JS_์‹ฌํ™”

๋ชฉ๋ก ๋ณด๊ธฐ
17/17

๐Ÿ’ฏ๏ธDOM์„ ์•„์ง๋„ ์ž˜ ๋‹ค๋ฃจ์ง€ ๋ชปํ•ด์„œ ๋‹ค์‹œ ๊ณต๋ถ€ํ•œ๋‹ค๐Ÿ’ฏ๏ธ
๐Ÿ’ฏ๏ธ์กฐ๊ธˆ์”ฉ ์ต์ˆ™ํ•ด์ง€๊ณ  ๋งŽ์ด ๋งŒ๋“ค์–ด๋ณด์ž๐Ÿ’ฏ๏ธ
๐Ÿ’ฏ๏ธํ™”์ดํŒ…๐Ÿ’ฏ๏ธ

DOM (Document Object Model)

  • HTML ๋ฌธ์„œ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ์ผ์ข…์˜ ์ธํ„ฐํŽ˜์ด์Šค
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ†ตํ•ด html์„ ์ œ์–ด ๋ฐ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ model

DOM ํŠธ๋ฆฌ๊ตฌ์กฐ

  • ๊ฐ๊ฐ ํ•˜๋‚˜(์˜ ๋…ธ๋“œ)๋ฅผ JS์˜ "๊ฐ์ฒด"๋กœ ๋ณธ๋‹ค.
  • ๊ฐ์ฒด๋กœ ๋ณด๊ธฐ ๋•Œ๋ฌธ์—, ์ด ๊ฐ ๋…ธ๋“œ๋ฅผ JS๋กœ ์กฐ์ž‘ ๋ฐ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค.

document ๊ฐ์ฒด

  • Document ๊ฐ์ฒด๋Š” "์›น ํŽ˜์ด์ง€ ๊ทธ ์ž์ฒด"๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  • ์›น ํŽ˜์ด์ง€์— ์กด์žฌํ•˜๋Š” HTML ์š”์†Œ๋ฅผ ์กฐ์ž‘ํ•˜๋ ค๊ณ  ํ•  ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ document ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

document ๊ฐ์ฒด(์˜ ๋‹ค์–‘ํ•œ) methods

dom ์š”์†Œ ์ œ์–ด

  • ๋งŽ์ด ์“ฐ๋Š” ๊ฒƒ or ํ‰์†Œ์— ํ–‡๊ฐˆ๋ฆฌ๋˜ ๊ฒƒ ์œ„์ฃผ๋กœ~

  • ํƒœ๊ทธ ๋‚ด ํƒœ๊ทธ ์ฐพ๊ธฐ

<h1 id="main-title">
    DOM (Document Object Model)
    <small>small</small>
</h1>
#main-title small{
    color: rgb(243, 243, 243);
let mainTitle = document.querySelector("#main-title small")
console.log(mainTitle)
}
  • querySelector
<ul>
    <li class = "willy"><img src="file:///home/jungeundelilahlee/%EC%82%AC%EC%A7%84/willy/beauty_20201114211020(1).jpg"></li>
    <li class = "willy"><img src="file:///home/jungeundelilahlee/%EC%82%AC%EC%A7%84/willy/beauty_20200502110303.jpg"></li>
    <li class = "willy"><img src="file:///home/jungeundelilahlee/%EC%82%AC%EC%A7%84/willy/beauty_20200229130644.jpg"></li>
</ul>
let willy = document.querySelectorAll(".willy") // ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
for (let i = 0; i < willy.length; i++) {
    willy[i].style.border = "3px solid pink"
}

console.log(willy[1]) // ๋‘๋ฒˆ์งธ ์‚ฌ์ง„
window.addEventListener("click", function () {
   let title = document.getElementById("main-title")
   let titleSpan = title.getElementsByTagName("span")
   console.log(title)
   console.log(titleSpan[0])
})
  • classList , createElement, appendChild
<ul>
    <li class = "willy"><img src="file:///home/jungeundelilahlee/%EC%82%AC%EC%A7%84/willy/beauty_20201114211020(1).jpg"></li>
    <li class = "willy"><img src="file:///home/jungeundelilahlee/%EC%82%AC%EC%A7%84/willy/beauty_20200502110303.jpg">
      <a class = "link-google"href="https://google.com">Google</a>
    </li>
    <li class = "willy"><img src="file:///home/jungeundelilahlee/%EC%82%AC%EC%A7%84/willy/beauty_20200229130644.jpg"></li>
</ul>
// class ์ถ”๊ฐ€ ๋ฐ ์‚ญ์ œ

let link = document.querySelector(".willy a")
link.className = "special" 
//! className์„ special๋กœ ๋ฐ”๊พผ ๊ฒƒ
//! ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ๋งŒ ํ•  ์ˆ˜๋Š” ์—†์„๊นŒ? (์‚ฌ์‹ค ์ž˜ ์•ˆ์”€)

link.classList.add("special", "special2")
//! ์›๋ž˜์˜ ํด๋ž˜์Šค์— ์›ํ•˜๋Š” ๊ฒƒ๋งŒ ์ถ”๊ฐ€
//! ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ์ธ์ž๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ

setTimeout(function () {
    link.classList.remove("special")
}, 2000)
//! ์‚ญ์ œ

console.log(link.classList.contains("special"))
//! class์— ํŠน์ • ์š”์†Œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€ ํŒ๋‹จ 
//! ๋ถˆ๋ฆฌ์–ธ ๊ฐ’
-------------------------------------------------------

// (์กด์žฌํ•˜์ง€ ์•Š๋˜) ์ปจํ…์ธ ๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•ด์„œ ์กฐ๋ฆฝํ•˜๋Š” ์ž‘์—…

// 2. ์ƒ์„ฑํ•  ์ปจํ…์ธ  ๋งŒ๋“ค๊ธฐ
let listEl = document.createElement("li")
listEl.innerHTML = "<span>willy is</span>"

// 1. ๋„ฃ๊ธฐ (๋ˆ„๊ฐ€? ์—„๋งˆ๊ฐ€)  
// 1-1. ์—„๋งˆ ๊ฐ€์ ธ์˜ค๊ธฐ
let willyMom = document.querySelector(".willy-mom");
willyMom.appendChild(listEl)

event target


// ๋งŽ์ด ์“ฐ์ง€๋งŒ, ์†๋„ ๋ฌธ์ œ๊ฐ€ ์žˆ์Œ
let menuLinks = document.querySelectorAll(".menu-link")
function clickMenuHandler() {
     let activeMenu = document.querySelector(".menu-active")
     if (activeMenu) {
         activeMenu.classList.remove("menu-active")
     }
     this.classList.add("menu-active") // this๋กœ ํด๋ฆญํ•œ ๊ฒƒ์„ ์„ ํƒ&๋ณ€ํ™”
}
for (let i = 0; i < menuLinks.length; i++) {
     menuLinks[i].addEventListener("click", clickMenuHandler)
}

--------------------------------------------------------

// ์ข€๋” ์†๋„ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋œ ๋ฐฉ๋ฒ•
let currentMenu; // ํ˜„์žฌ ํ™œ์„ฑํ™”๋œ ๋ฉ”๋‰ด๋ฅผ ๋‹ด๋Š” ๋ณ€์ˆ˜ (๋‹ด์•„๋†จ๋‹ค๊ฐ€ ์ œ๊ฑฐ -> ๋ฐ˜๋ณต)
let menuLinks = document.querySelectorAll(".menu-link")
function clickMenuHandler() {
     if (currentMenu) {
         currentMenu.classList.remove("menu-active")
     }
     this.classList.add("menu-active")
     currentMenu = this // this๋กœ ํด๋ฆญํ•œ ๊ฒƒ์„ ์„ ํƒ&๋ณ€ํ™”
     console.log(currentMenu)
}
for (let i = 0; i < menuLinks.length; i++) {
     menuLinks[i].addEventListener("click", clickMenuHandler)
}

--------------------------------------------------------

// ์ด๋ฒคํŠธ ํƒ€๊ฒŸ
let curruntMenu;
let menu = document.querySelector(".menu")
function clickHandler (e) {
    if (curruntMenu) {
        curruntMenu.textContent = ""
        curruntMenu.classList.remove("menu-active")
    }
    e.target.classList.add("menu-active")
    curruntMenu = e.target
}
menu.addEventListener("click", clickHandler)
profile
delilah's journey

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