๐กdocument.querySelector(""); ๋ฅผ ํตํด์ html์์๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ ์์๊ณ , ๊ทธ ์์์ ๋ด๋ถ๋ฅผ ๋ณด๊ณ ์ถ๋ค๋ฉด, console.log() ๋์ console.dir()์ ์ฌ์ฉํ๋ฉด๋๋ค.(๊ฐ๋ฐ์๋๊ตฌ์์ ์ฌ์ฉํ๋ฉด ์ฉ์ดํจ)
<div class="hello">
<h1>Grab me!</h1>
<h1>Grab me123!</h1>
<h1>Grab me345!</h1>
<h1>Grab me567!</h1>
</div>
์ ๊ฐ์ htmlํ์ผ์ด ์๋ค๊ณ ๊ฐ์ ํ๊ณ ,
JS์ฝ๋๋ฅผ ์์ฑํด elements์ property๋ฅผ ๋ณด๊ณ ์ถ๋ค๋ฉด,
const title = document.querySelector(".hello:first-child h1");
console.dir(title);
์์ ๊ฐ์ด ํน์ element๋ฅผ ์ง์ ํ๋ค์ dir์ ์ฌ์ฉํด ํธ์ถํ๋ค.
๐กconsole.dir()์ ์ฌ์ฉํด์ objectํ๋ ์์์ elements๋ฅผ ๋ณด๊ฒ๋๋ฉด, onxxxx๋ผ๊ณ ์ ํ์๋๊ฒ์ ๋ณผ ์ ์๋๋ฐ, ๋ชจ๋ events์ ํด๋น๋๋ค.
๐กobject์์ object๊ฐ ๋ค์ด๊ฐ ์ ์๋๋ฐ ๊ทธ ์ค ์ ๊ธฐํ๊ฑด style๋ ์์์ object์์ ์๋ object์ด๋ค. style object์ elements๋ฅผ ๋ณด๋ฉด JS๋ก ์์ฑ๋์ด ์๋๊ฑธ ํ์ธํ ์ ์๋๋ฐ, ์ด๋ ์ฆ JS๋ก CSS style์ ์กฐ์๊ฐ๋ฅํ๋ค๋ ๋ป์ด๋ค.
title.style.color = "blue";
//ํ๋์์ผ๋ก ๋ณ๊ฒฝ๋๊ฒํ๋ JS ๋ฏธ์น๋์ด๋ค?
๐ก events๋ ๋ง๊ทธ๋๋ก ํจ๊ณผ๋ค. ๋๋ฅด๊ฑฐ๋, ์ฌ์ฉ์๊ฐ ์
๋ ฅ์ ํ๋ค๊ฑฐ๋, ์ ์ฅํ๋ค๋์ง, ๋ง์ฐ์ค๋ฅผ ๊ฐ์ ธ๋ค ๋๋ค๊ฑฐ๋...
function handleTitleClick() {
title.style.color = "blue";
}
function handleMouseEnter() {
title.innerHTML = "mouse is here!";
}
function handleMouseLeave() {
title.innerHTML = "mouse is gone!";
}
title.addEventListener("click", handleTitleClick);
// addEventListener์ด "click" ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์๋ ํจ์ handleTitleClicked๊ฐ ์คํ์์ผ์ง๋๋ก ๋ง๋ฌ!
์์ ์ฝ๋๋ฅผ ์๋์ ๊ฐ์ด ์ธ ์ ์๋ค.
title.onclick = handleTitleClick;
title.addEventListener("mouseenter", handleMouseEnter);
title.onmouseenter = handleMouseEnter;
title.addEventListener("mouseleave", handleMouseLeave);
addEventListener ์ ๋ ์ ํธํ๋ ์ด์ ๋ .removeEventListener ์ ์ฌ์ฉํ ์ ์๊ณ ๋์ค์ ์ฝ๋๊ฐ ๋ ๊น๋ํด์ง๋ ์ฅ์ ์ด ์๋ค.
function handleWindowSize() {
document.body.style.backgroundColor = "tomato";
}
function handleWindowCopy() {
alert("copier!!!!!!!!!!!!!!");
}
function handleWindowOffLine() {
alert("SOS no wifi!");
}
function handleWindowOnLine() {
alert("ALL GOOOOOOOODD!!");
}
window.addEventListener("resize", handleWindowSize);
//์๋์ฐ ์ฐฝ ํฌ๊ธฐ ์์ ๋ ๋!
window.addEventListener("copy", handleWindowCopy);
//๋๊ตฐ๊ฐ๊ฐ control+c ์๋ํ์๋!
window.addEventListener("offline", handleWindowOffLine);
//์์ดํ์ด ํฐ์ก์๋!
window.addEventListener("online", handleWindowOnLine);
//์์ดํ์ด ์ฐ๊ฒฐ๋์์๋!
๊ทธ์ธ ์๋์ฐ์ ํจ๊ณผ๋ฅผ ์ฃผ๋ ๋ฐฉ๋ฒ๋ ์๋ค.