Function Practice
1. DOM과 조건문 (if~else)
1-1. JS코드만으로 Style과 클릭변경 효과주기
const title = document.querySelector("#title");
const BASE_COLOR = "rgb(52, 73, 94)";
const OTHER_COLOR = "red";
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); //원할 때 handleClick 호출
}
init(); //타이틀을 클릭할 때 마다 회색-빨강 교차적으로 글색상이 변경됨1-2. CSS에 Style을 주어 JS코드 깔끔히 정리
1-3. 토글함수를 이용한 가장 깔끔한 코드
2. Online / Offline (와이파이 껐다 켜기)
Last updated
Was this helpful?