๐Ÿ“ƒ๋…ธ๋งˆ๋“œ์ฝ”๋” Vanilla JS ๋ฉ”๋ชจ2

์†Œ์—ฐยท2021๋…„ 6์›” 30์ผ
0

vanillaJS

๋ชฉ๋ก ๋ณด๊ธฐ
3/8

onmouseEvent

const h1 = document.querySelector(".hello h1");
h1.innerText = "Click Me"

function handleTitleClick(){
    console.log("title was clicked!")
    h1.style.color = "blue";
}
function handleTitleMouseEnter(){
    h1.innerText = "Mouse is here!";
}
function handleMouseLeave(){
    h1.innerText = "Mouse is gone";
}

h1.addEventListener("click", handleTitleClick);
h1.addEventListener("mouseenter", handleTitleMouseEnter);
h1.addEventListener("mouseleave", handleMouseLeave);

h1 ๋ณ€์ˆ˜ ์„ ์–ธ(class="hello"์˜ h1 ํƒœ๊ทธ ์„ ํƒ)
h1์˜ innerText๋ฅผ "Click Me"๋กœ ๋ณ€๊ฒฝ

addEventListener๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ "click", "mouseenter", "mouseleave" ๋“ฑ์˜ ์ด๋ฒคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์‹คํ–‰.

h1.onclick = handleTitleClick; ๊ณผ
h1.addEventListener("click", handleTitleClick); ์€ ๊ฐ™์€ ๋™์ž‘์„ ํ•จ.
ํ›„์ž๊ฐ€ ๋‚˜์ค‘์— removeEventListener๋ฅผ ํ†ตํ•ด event listener๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์–ด์„œ ๋” ์„ ํ˜ธ


function handleWindowResize(){
    document.body.style.backgroundColor = "tomato";
}
function handleWindowCopy(){
    alert("copier!");
}

window.addEventListener("resize", handleWindowResize);
window.addEventListener("copy", handleWindowCopy);

resize์ด๋ฒคํŠธ๋ฅผ ํ†ตํ•ด window ์ฐฝ์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋ฐ”๋€” ๋•Œ ์ƒ‰์ƒ ๋ณ€๊ฒฝ("resize")
document.body์— bgckground color๊ฐ’์„ ์คŒ.
๋ณต์‚ฌ๋ฅผ ์ธ์‹ํ•˜๋Š” copy์ด๋ฒคํŠธ๋„ ์žˆ์Œ("copy")


function handleWindowOffLine(){
    alert("SOS no WIFI");
}
function hadleWindowOnLine(){
    alert("All Good!");
}

window.addEventListener("offline", hadleWindowOffLine);
window.addEventListener("online", handleWindowOnLine);

์™€์ดํŒŒ์ด ์—ฐ๊ฒฐ์„ ์ธ์‹ํ•˜๋Š” ์ด๋ฒคํŠธ("offLine", "onLine")
๋‚˜๋Š” ์™œ ์•ˆ๋˜์ง€..

profile
์ฝ”๋ฆฐ์ด๐Ÿคช

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