티스토리 뷰

이론/jQuery

[jQuery] .addClass() .removeClass()

millfeel1298 2021. 10. 24. 00:49

See the Pen [jQuery] .addClass() .removeClass() by mill (@millfeel) on CodePen.

 

특정 요소에 클래스를 추가하고 제거하는 구문이다.

// 클래스 추가하기
$('선택자').addClass('추가할 클래스명');

// 클래스 제거하기
$('선택자').removeClass('제거할 클래스명');

 

[JS와 비교하기]

// JS
const $box = document.querySelector('.box');

$box.addEventListener('mouseover', function () {
  this.classList.add('box__circle');
});
$box.addEventListener('mouseout', function () {
  this.classList.remove('box__circle');
});
// jQuery
$(document).ready(function () {
  $('.box').mouseover(function(){
    $(this).addClass('box__circle');
  }).mouseout(function(){
    $(this).removeClass('box__circle');
  });
});

[공부좌표]

인터랙티브 웹디자인북 p145

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함