1. Searching For Elements
1-1) HTML
1-2) Javascript
const title = document.querySelector(".hello h1");
console.log(title);
동작 원리
Console
2. Click Events Part One
2-1) addEventListener()
// title element를 찾아오기
const h1 = document.querySelector("div.hello:first-child h1");
function handleTitleClick() {
console.log("title was clicked");
h1.style.color = "blue"; // 클릭 후 파란색으로 변함.
}
// function을 실행시키고 싶으면
h1.addEventListener("click", handleTitleClick);
// 중요 포인트
handlerTitleClick(); // 실행할 function을 넣지 않아도 됨.
동작 원리
Console
2-2) 동작 원리
3. Click Events Part Two
3-1) h1 html element mdn으로 검색하기
console.dir(title); // 세부사항을 볼 수 있음.
Console
3-2) mouseEnter function과 mouseLeave 만들기
const h1 = document.querySelector("div.hello:first-child h1");
function handleMouseEnter() {
h1.innerText = "Mouse is here!!!";
}
function handleMouseLeave() {
h1.innerText = "Mouse is gone.";
}
h1.addEventListener("mouseenter", handleMouseEnter);
h1.addEventListener("mouseleave", handleMouseLeave);
Console
4. More Events Window(document.body)
4-1) HTML
document.body
Q : document.div는 가져올 수 없을까?
4-2) Javascript
const h1 = document.querySelector("div.hello:first-child h1");
function handleMouseClick() {
h1.style.color = "blue";
}
function handleWindowResize() {
document.body.style.backgroundcolor = "tomato";
}
h1.addEventListener("click", handleMouseClick);
window.addEventListener("resize", handelWindowResize);
Console
HTML
5. More Events Window-2(alert)
5-1) Javascirpt
function handleWindowOffline() {
alert("SOS no WIFI");
}
window.addEventListener("offline", handleWindowOffline);
Console
5-2) Javacript
function handleWinodwOnline() {
alert("ALL GOOD");
}
window.addEventListener("online", handleWindowOnline);
Console
6. className
6-1) 기본
function handleTitleClick() {
if (h1.style.color === "blue) {
h1.style.color = "tomato";
} else {
h1.style.color = "blue";
}
}
h1.addEventListener("click", handleTitleClick);
6-2) 간결하게 바꾸기
const h1 = document.querySelector("div.hello:first-child h1");
function handleTitleClick() {
const currentColor = h1.style.color;
let newColor;
if (currentColor === "bule") {
newColor = "tomato";
} else {
newColor = "blue";
}
h1.style.color = newColor;
}
h1.addListener("click", handleTitleClick);
6-3) 더 간결하게 바꾸기
7. classList()