Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags more
Archives
Today
Total
관리 메뉴

기록

자바스크립트 토글 본문

JAVASCRIPT&JQUERY

자바스크립트 토글

9400 2023. 9. 20. 17:15
<body>
    <div class="hello">
    <h1 class="sexy-font">바뀌는값</h1>
    </div>
    <script src="app.js"></script>
</body>

 

 

const h1 = document.querySelector("div.hello:first-child h1");

function handleTitleClick(){
    const clickedClass = "active";
    if(h1.classList.contains(clickedClass)){
        h1.classList.remove(clickedClass);
    } else {
        h1.classList.add(clickedClass);
    }
}

h1.addEventListener("click",handleTitleClick);
const h1 = document.querySelector("div.hello:first-child h1");

function handleTitleClick(){
    h1.classList.toggle("active");
}

h1.addEventListener("click",handleTitleClick);

 

body {
    background-color: beige;
}

h1{
    color : blue;
    transition: color 0.5s ease-in-out;
}

.active{
    color : tomato;
}

.sexy-font{
    font-family: 'Courier New', Courier, monospace;
}
Comments