프론트/javascript

javascript eventListener 방법 메모

임진묵식뽕뽑기 2022. 8. 30. 13:39
title.addEventListener("click", handleTitleClick);
//.removeEventListener 이런게 가능해서 addEventListener 선호
//title.onclick = handleTitleClick;

 

function handleTitleClick() {
  const clickedClass = "clicked sexy-font";
  if (h1.classList.contains(clickedClass)) {
    h1.className = "";
  } else {
    h1.className = clickedClass;
  }
}
h1.addEventListener("click", handleTitleClick);
 
 
 
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Momentum</title>
</head>

<body>
    <div class="hello">
        <h1 class="sexy-font">Click me!</h1>
    </div>
    <script src="app.js"></script>
</body>

</html>
 
 

classList ==>>

Allows for manipulation of element's class content attribute as a set of whitespace-separated tokens through a DOMTokenList object.

 

add, remove 가능!!!!!

 

 

toggle. 쓰면  한줄로 가능