๋ฐ์ํ
[์ถ์ฒ-์ ํ๋ธ ๋ ธ๋ง๋ ์ฝ๋ Nomad Coders]
index.js
const title = document.querySelector("#title");
const CLICKED_CLASS = "clicked";
function handleClick(){
const currentClass = title.className;
if(currentClass !== CLICKED_CLASS){
title.className = CLICKED_CLASS;
} else {
title.className = "";
}
}
function init(){
title.addEventListener("click", handleClick);
}
init();
index.css (css๋ ๊ณ ์ )
body {
background-color: #ecf0f1;
}
.btn {
cursor: pointer;
}
h1 {
color: #34495e;
transition: color 0.5s ease-in-out;
}
.clicked{
color: #7f8c8d;
}
const title = document.querySelector("#title");
const CLICKED_CLASS = "clicked";
function handleClick(){
const currentClass = title.className;
if(currentClass !== CLICKED_CLASS){
title.classList.add(CLICKED_CLASS);
} else {
title.classList.remove(CLICKED_CLASS);
}
}
function init(){
title.addEventListener("click", handleClick);
}
init();
-toggle์ on/off ์ค์์น์ ๋น์ทํ๋ค๊ณ ๋ณผ ์ ์๋ค
const title = document.querySelector("#title");
const CLICKED_CLASS = "clicked";
function handleClick() {
title.classList.toggle(CLICKED_CLASS);
}
function init(){
title.addEventListener("click", handleClick);
}
init();
๋ฐ์ํ
๋๊ธ