๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JavaScript/Vanilla JS

[๋ฐ”๋‹๋ผJS๋กœ ํฌ๋กฌ ์•ฑ ๋งŒ๋“ค๊ธฐ] DOM - If else - Function practice

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 26.
๋ฐ˜์‘ํ˜•

[์ถœ์ฒ˜-์œ ํŠœ๋ธŒ ๋…ธ๋งˆ๋“œ ์ฝ”๋” Nomad Coders]

https://youtu.be/UwnBvuFyiBU

-์ด๋ฒคํŠธ์˜ ๊ทผ์›์„ ์•Œ๊ณ  ์‹ถ์œผ๋ฉด ํ•ญ์ƒ MDN์„ ์ฐพ์•„๋ณด๋ผ

 

const title = document.querySelector("#title");

const BASE_COLOR = "rgb(52, 73, 94)";
const OTHER_COLOR = "#7f8c8d";

function handleClick(){
    const currentColor = title.style.color;
    console.log(currentColor);
}

function init(){
    title.style.color = BASE_COLOR;
    title.addEventListener("click", handleClick);
}

init();

 

 

const title = document.querySelector("#title");

const BASE_COLOR = "rgb(52, 73, 94)";
const OTHER_COLOR = "#7f8c8d";

function handleClick(){
    const currentColor = title.style.color;
    if (currentColor === BASE_COLOR){
        title.style.color = OTHER_COLOR;
    } else {
        title.style.color = BASE_COLOR;
    }
}

function init(){
    title.style.color = BASE_COLOR;
    title.addEventListener("click", handleClick);
}

init();

 

 

function init(){
    title.style.color = BASE_COLOR;
    title.addEventListener("mouseenter", handleClick);
}

-mouseenter๋กœ ๋ฐ”๊พธ๋ฉด ๋งˆ์šฐ์Šค๋ฅผ ์˜ฌ๋ฆด๋•Œ๋งˆ๋‹ค ์ƒ‰๊น”์ด ๋ฐ”๋€๋‹ค

 

 

function handleOffline(){
    console.log("lalala");
}

window.addEventListener("offline", handleOffline)

-์˜คํ”„๋ผ์ธ์ผ ๋•Œ(์ธํ„ฐ๋„ท ์—ฐ๊ฒฐ์ด ์•ˆ๋˜์—ˆ์„๋•Œ) 'lalala'๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค

 

 

function handleOffline(){
    console.log("bye bye");
}

function handleOnline(){
    console.log("welcome back");
}

window.addEventListener("offline", handleOffline)
window.addEventListener("online", handleOnline)

-์˜จ๋ผ์ธ์ผ ๋•Œ 'welcome back'์„ ์ถœ๋ ฅํ•˜๊ณ  ์˜คํ”„๋ผ์ธ์ผ ๋•Œ 'bye bye'๋ฅผ ์ถœ๋ ฅ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€