๐ฏ๏ธ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]) // ๋๋ฒ์งธ ์ฌ์ง
addEventListener
: ๋งค~์ฐ ๋ค์ํ ์ด๋ฒคํธ ์ข
๋ฅ๋ ์ฌ๊ธฐ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)
// ๋ง์ด ์ฐ์ง๋ง, ์๋ ๋ฌธ์ ๊ฐ ์์
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)