<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Momentum App</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1 class="sexy-font">Click me!</h1>
</div>
<script src="app.js"></script>
</body>
</html>
CSS
.active {
color: tomato;
transition: color 0.5s ease-in-out;
}
.sexy-font {
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}
const clickText = document.querySelector(".container h1");
const handleClick = () => {
const clickedClass = "active"
if (clickText.className === clickedClass) {
clickText.className = "";
} else {
clickText.className = clickedClass;
}
}
clickText.addEventListener("click", handleClick)
const clickText = document.querySelector(".container h1");
const handleClick = () => {
const ClickedClass = "active"
if (clickText.classList.contains(ClickedClass)) {
clickText.className = "";
} else {
clickText.className = ClickedClass;
}
}
clickText.addEventListener("click", handleClick)
JS
const clickText = document.querySelector(".container h1");
const handleClick = () => {
const ClickedClass = "active"
if (clickText.classList.contains(ClickedClass)) {
clickText.classList.remove(ClickedClass);
} else {
clickText.classList.add(ClickedClass);
}
}
clickText.addEventListener("click", handleClick)
결과:
const clickText = document.querySelector(".container h1");
const handleClick = () => {
clickText.classList.toggle("active")
}
clickText.addEventListener("click", handleClick)