Web

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ classList๋ฅผ ์จ์•ผํ•˜๋Š” ์ด์œ 

๋…ธ๋ฃจ๋ฃฝ 2021. 1. 16. 21:19

~์ž ๊น ์‚ฌ๋‹ด~

์•„์ง ๊ณต๋ถ€ํ•œ ์ง€ ์–ผ๋งˆ ์•ˆ ๋์ง€๋งŒ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋„ˆ๋ฌด ์–ด๋ ต๋‹ค.ใ… ใ… 

ํŒŒ์ด์ฌ, ์ž๋ฐ”๋ฅผ ์ฒ˜์Œ ๋ฐฐ์šธ๋•Œ ๋ณด๋‹ค ํ›จ์”ฌ ์–ด๋ ค์›€....ใ… ใ… ใ… ใ… 

๊ฒŒ๋‹ค๊ฐ€ ์ง€๊ธˆ ์ฑŒ๋ฆฐ์ง€ ์ค‘์ธ๋ฐ, ์ž˜ํ•˜์‹œ๋Š” ๋ถ„๋“ค ๋„ˆ๋ฌด ๋งŽ์•„์„œ ์ž๊ดด๊ฐ ๋“ ๋‹ค. ์ง„์งœ ๋ฆฌ์ŠคํŽ™......๐Ÿ˜ญ

 

if๋ฌธ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

html

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Review</title>
    <link rel="stylesheet" href="index.css" />
</head>
<body>
    <h1 id = "title">JS is so hard!!</h1>
    <script src="index.js"></script>
</body>
</html>

css

body {
  background-color: #fab1a0;
}

h1 {
  color: #2980b9;
  transition: color 0.5s ease-in-out;
}

.clicked {
  color: #6c5ce7;
}

JS

const title = document.querySelector("#title");

const CLICKED_CLASS = "clicked";

function handleClick(){
    const currentClass = title.className;
    console.log(currentClass);

    if(currentClass !== CLICKED_CLASS){
        title.className = CLICKED_CLASS;
    }
    else{
        title.className = "";
    }
}

function init(){
    title.addEventListener("click", handleClick);
}

init();

css์—๋Š” ๊พธ๋ฏธ๋Š” ๊ธฐ๋Šฅ๋งŒ, js์—์„œ ๋™์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ๋งŒ ๋„ฃ์€ ์ƒํƒœ์ด๋‹ค.

ํด๋ฆญ ์‹œ, h1์˜ class๊ฐ€ ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ์ƒํƒœ์—์„œ class="click"์œผ๋กœ ๋ฐ”๋€๋‹ค.

 

๋ฌธ์ œ์ 

ํ•˜์ง€๋งŒ, ์ด๊ฒƒ์€ ํ˜„์žฌ h1์˜ class๊ฐ€ ์—†๋Š” ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ •์ƒ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

html์˜ h1์— class="btn"์„ ์ถ”๊ฐ€ํ•˜๊ณ ,

css์—์„œ .btn{cursor: pointer}๋ฅผ ์ถ”๊ฐ€ํ•˜์ž. (์ปค์„œ๊ฐ€ ์†๊ฐ€๋ฝ ๋ชจ์–‘์œผ๋กœ ๋‚˜ํƒ€๋‚˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ.)

class๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ์š” ์—†์—ˆ์Šต๋‹ˆ๋‹ค.

2๋ฒˆ ํด๋ฆญํ•˜๋ฉด class๊ฐ€ 

"btn" -> "click" -> ""

์˜ ์ƒํƒœ๋กœ ๋ฐ”๋€๋‹ค.

์ฆ‰, ๊ธฐ์กด์˜ ํด๋ž˜์Šค๊ฐ€ ์‚ฌ๋ผ์ง€๊ฒŒ ๋˜๋Š” ๋ฌธ์ œ ๋ฐœ์ƒ!๐Ÿ’ฆ

 

์ด์ฒ˜๋Ÿผ replace ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด classList๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

classList - Methods

  • add(String)
    • ์ง€์ •ํ•œ ํด๋ž˜์Šค ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
    • ๋งŒ์•ฝ ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ํด๋ž˜์Šค๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•œ๋‹ค๋ฉด ๋ฌด์‹œ.

 

  • remove(String)
    • ์ง€์ •ํ•œ ํด๋ž˜์Šค ๊ฐ’์„ ์ œ๊ฑฐํ•œ๋‹ค.
    • ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํด๋ž˜์Šค๋ผ๋ฉด? ์—๋Ÿฌ ๋ฐœ์ƒ X

 

  • contains(String)
    • ์ง€์ •ํ•œ ํด๋ž˜์Šค ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ.
    • true, false ๊ฐ’์„ ๋ฐ˜ํ™˜.

 

  • replace(old, new)
    • old class๋ฅผ new class๋กœ ๋Œ€์ฒด

 

  • item(Number)
    • ์ธ๋ฑ์Šค ๊ฐ’์„ ํ™œ์šฉํ•˜์—ฌ ํด๋ž˜์Šค ๊ฐ’์„ ๋ฐ˜ํ™˜

 

const title = document.querySelector("#title");

const CLICKED_CLASS = "clicked";

function handleClick(){
    const hasClass = title.classList.contains(CLICKED_CLASS);

    if(!hasClass){
        title.classList.add(CLICKED_CLASS);
    }
    else{
        title.classList.remove(CLICKED_CLASS);
    }
}

function init(){
    title.addEventListener("click", handleClick);
}

init();

classList๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ฝ”๋“œ ์žฌ์ž‘์„ฑ.

์ •์ƒ์ ์œผ๋กœ ์ž‘๋™๋œ๋‹ค! ๐Ÿ‘

ํ•˜์ง€๋งŒ, ์ด๊ฒƒ๋„ ๋”์šฑ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

๋ฐ”๋กœ๋ฐ”๋กœ toggle ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ!!!!!!

 

classList - toggle

toggle์€ ํด๋ž˜์Šค์˜ ์œ ๋ฌด๋ฅผ ์ฒดํฌํ•ด์„œ ์—†์œผ๋ฉด add, ์žˆ์œผ๋ฉด remove๋ฅผ ์ž๋™์œผ๋กœ ์‹œ์ผœ์ค€๋‹ค.

์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๋ฐ”๋กœ ์œ„์˜ ์ฝ”๋“œ์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค.

 

const title = document.querySelector("#title");

const CLICKED_CLASS = "clicked";

function handleClick(){
    title.classList.toggle(CLICKED_CLASS);
}

function init(){
    title.addEventListener("click", handleClick);
}

init();

๋„˜๋‚˜ ํŽธ๋ฆฌํ•œ ๊ฒƒ!!๐Ÿ˜Ž