jQuery 토글 클래스() 메서드

❮ jQuery HTML/CSS 메소드

예시

모든 <p> 요소에 대한 "기본" 클래스 이름 추가 및 제거 사이를 전환합니다.

$("button").click(function(){
  $("p").toggleClass("main");
});

정의 및 사용

toggleClass() 메서드는 선택한 요소에서 하나 이상의 클래스 이름을 추가하거나 제거하는 작업을 토글합니다.

이 메서드는 지정된 클래스 이름에 대한 각 요소를 확인합니다. 클래스 이름이 없으면 추가되고 이미 설정되어 있으면 제거됩니다. - 토글 효과가 생성됩니다.

그러나 "switch" 매개변수를 사용하여 클래스 이름만 제거하거나 추가하도록 지정할 수 있습니다.


통사론

$(selector).toggleClass(classname,function(index,currentclass),switch)

Parameter Description
classname Required. Specifies one or more class names to add or remove. To specify several classes, separate the class names with a space
function(index,currentclass) Optional. Specifies a function that returns one or more class names to add/remove
  • index - Returns the index position of the element in the set
  • currentclass - Returns current class name of selected elements
switch Optional. A Boolean value specifying if the class should only be added (true), or only be removed (false)

직접 해보십시오 - 예


간 전환 toggleClass() 메서드를 사용하여 클래스 이름 추가 및 제거 사이를 전환하는 방법.


를 사용하여 선택한 요소에 대해 토글해야 하는 클래스 이름을 지정합니다.


를 사용하여 클래스 이름만 추가하거나 제거하는 방법.


❮ jQuery HTML/CSS 메소드