classList

kirin.logΒ·2021λ…„ 1μ›” 27일
0

classList

πŸš€ μš”μ†Œμ—μ„œ css클래슀λ₯Ό μΆ”κ°€, 제거, μ „ν™˜ν•˜λŠ”λ° μœ μš©ν•˜κ²Œ 쓰인닀.
πŸš€ μœ„ 속성은 읽기 μ „μš©μ΄μ§€λ§Œ, add() 및 remove() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μˆ˜μ •ν•  수 μžˆλ‹€

🚩 add ➑ 1개 λ˜λŠ” μ—¬λŸ¬κ°œμ˜ class 속성을 μΆ”κ°€

document.getElementById("myDIV").classList.add("mystyle");
document.getElementById("myDIV").classList.add("mystyle", "anotherClass");
document.getElementById("myDIV").classList.add("mystyle", "anotherClass", "thirdClass");

μ˜ˆμ‹œ

<body>
  <button onclick="myFunction()">Try it</button>

  <div id="myDIV">
  	I am a DIV element
  </div>
<body>
<style>
.mystyle {
  width: 500px;
  height: 50px;
  padding: 15px;
  border: 1px solid black;
}

.anotherClass {
  background-color: coral;
  color: white;
}

.thirdClass {
  text-transform: uppercase;
  text-align: center;
  font-size: 25px;
}
</style>
<script>
function myFunction() {
  document.getElementById("myDIV").classList.add("mystyle", "anotherClass", "thirdClass");
}
</script>

🚩 remove ➑ 1개 λ˜λŠ” μ—¬λŸ¬κ°œμ˜ class 속성을 제거

document.getElementById("myDIV").classList.remove("mystyle");
document.getElementById("myDIV").classList.remove("mystyle", "anotherClass");
document.getElementById("myDIV").classList.remove("mystyle", "anotherClass", "thirdClass");
// κΈ°μ‘΄ μ μš©λ˜μ–΄ μžˆλŠ” 속성 μ€‘μ—μ„œ λΉΌκΈ°

μ˜ˆμ‹œ

<body>
  <button onclick="myFunction()">Try it</button>

  <div id="myDIV" class="mystyle">
  	I am a DIV element
  </div>
<body>
<style>
.mystyle {
  width: 500px;
  height: 50px;
  padding: 15px;
  border: 1px solid black;
}
</style>
<script>
function myFunction() {
  document.getElementById("myDIV").classList.remove("mystyle");
}
</script>

🚩 toggle ➑ 두 class 클래슀 간을 μ „ν™˜ 제거

document.getElementById("myDIV").classList.toggle("newClassName");

μ˜ˆμ‹œ

<body>
  <button onclick="myFunction()">Try it</button>

  <div id="myDIV" class="mystyle">
  	I am a DIV element
  </div>
<body>
<style>
.mystyle {
  width: 300px;
  height: 50px;
  background-color: coral;
  color: white;
  font-size: 25px;
}

.newClassName {
  width: 400px;
  height: 100px;
  background-color: lightblue;
  text-align: center;
  font-size: 25px;
  color: navy;
  margin-bottom: 10px;
}
</style>
<script>
function myFunction() {
  document.getElementById("myDIV").classList.toggle("newClassName");
}
</script>

πŸš— classList 속성 및 λ©”μ†Œλ“œλ₯Ό ν™œμš©ν•œ μ˜ˆμ‹œ

πŸ‘‰ μš”μ†Œμ˜ class 이름을 κ°€μ Έμ˜€κΈ°.

<div id="myDIV" class="mystyle anotherClass thirdClass">I am a DIV element</div>

let x = document.getElementById("myDIV").classList;

πŸ‘‰ μš”μ†Œμ— μžˆλŠ” class 이름 수 ν™•μΈν•˜κΈ°.

let x = document.getElementById("myDIV").classList.length;

πŸ‘‰ μš”μ†Œμ˜ 첫 번째 클래슀 이름 (인덱슀 0)을 κ°€μ Έμ˜€κΈ°.

let x = document.getElementById("myDIV").classList.item(0);

πŸ‘‰ μš”μ†Œμ— "mystyle"ν΄λž˜μŠ€κ°€ μžˆλŠ”μ§€ ν™•μΈν•˜κΈ°.

let x = document.getElementById("myDIV").classList.contains("mystyle");

πŸ‘‰ μš”μ†Œμ— "mystyle"ν΄λž˜μŠ€κ°€ μžˆλŠ”μ§€ μ•Œμ•„λ³΄κ³ , μžˆλ‹€λ©΄ λ‹€λ₯Έ 클래슀 이름을 μ œκ±°ν•˜ν•˜κΈ°

let x = document.getElementById("myDIV");

if (x.classList.contains("mystyle")) {
       x.classList.remove("anotherClass");
} else {
       alert("Could not find it.");
}
profile
boma91@gmail.com

0개의 λŒ“κΈ€