JAVASCRIPT&JQUERY

자바스크립트 addEventListener로 글자색변경

9400 2023. 9. 20. 15:33
const h1 = document.querySelector("div.hello:first-child h1");

function handleTitleClick(){
    const currentColor = h1.style.color; //getter개념 -변수가 가지는 의미에 따라서 나눠서 선언
    let newColor; //setter개념 -변수가 가지는 의미에 따라서 나눠서 선언
    if(currentColor === "blue"){
        newColor = "tomato";
    }else{
        newColor = "blue";
    }
    h1.style.color = newColor;
}

h1.addEventListener("click",handleTitleClick);