💻
jQuery
  • 제이쿼리
  • 제이쿼리 기본
  • 선택자
    • 기본 선택자
    • 계층 선택자
    • 속성 선택자
    • 기본 필터 선택자
    • 내용 필터 선택자
    • 보임 필터 선택자
    • 자식 요소 필터 선택자
    • 폼 요소 필터 선택자
  • 탐색
    • find() / filter()
    • each() / $.each()
  • 속성
    • position() / offset()
    • addClass() / removeClass()
    • toggleClass()
    • hasClass()
    • Attr() / removeAttr()
    • scrollTop() / scrollLeft()
  • 변경
    • text() / html()
    • append() / prepend()
    • remove() / empty()
  • 애니메이션
    • show() / hide()
    • fadeIn() / fadeOut()
    • slideUp() / slideDown()
    • animate()
  • 이벤트
Powered by GitBook
On this page

Was this helpful?

  1. 속성

toggleClass()

toggleClass() 메서드는 선택한 요소에 지정한 클래스가 없으면 생성하고, 있는 경우에는 삭제합니다.

$("선택자").toggleClass("클래스 이름");

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQueray02 선택자</title>
    <style>
        @font-face {
            font-family: 'NEXON Lv2 Gothic';
            src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_20-04@2.1/NEXON Lv2 Gothic.woff') format('woff');
            font-weight: normal;
            font-style: normal;
        }
        body {font-family: 'NEXON Lv2 Gothic';}
        li.red {color:#c7254e; background: #f9f2f4; border: 1px dashed #a51a3d;}

    </style>
</head>
<body>

    <h1>탐색</h1>
    <ul>
        <li>addClass() 메서드는 선택한 요소에 클래스를 생성하고, removeClass() 메서드는 선택한 요소에서 지정한 클래스를 삭제합니다.</li>
        <li>toggleClass() 메서드는 선택한 요소에 지정한 클래스가 없으면 생성하고, 있는 경우에는 삭제합니다.</li>
        <li>hasClass() 메서드는 선택한 요소에 지정한 클래스가 있으면true를 반환하고, 없으면 false를 반환합니다.</li>
    </ul>
    <button class="btn1">addClass()</button>
    <button class="btn2">removeClass()</button>
    <button class="btn3">toggleClass()</button>
    
     <!-- script -->
     <script src="jquery.min_1.1.24.js"></script>
     
    <script>
        // $("li:odd").css({backgroundColor: "red", color: "#fff"});
        // $("li:nth-child(2)").css({backgroundColor: "red", color: "#fff"});
        // $("li:contains('toggle')").css({backgroundColor: "red", color: "#fff"});
        // $("li:eq(1)").css({backgroundColor: "red", color: "#fff"});
        // $("li:nth-of-type(2)").css({backgroundColor: "red", color: "#fff"});
        // $("li:nth-child(even)").css({backgroundColor: "red", color: "#fff"});
        // $("li:nth-child(2n)").css({backgroundColor: "red", color: "#fff"});

        $(".btn1").click(function(){
            // $("li:odd").css("backgroundColor", "red");
            $("li:odd").addClass("red");
        });

        $(".btn2").click(function(){
            $("li:odd").removeClass("red");
        });

        $(".btn3").click(function(){
            $("li:odd").toggleClass("red");
        });

    </script>

</body>
</html>
PreviousaddClass() / removeClass()NexthasClass()

Last updated 4 years ago

Was this helpful?