首页  >  属性  > toggleClass(class|fn[,sw]) 源码下载

返回值:jQuerytoggleClass(class|fn[,sw])

概述

如果存在(不存在)就删除(添加)一个类。

参数

classStringV1.0

CSS类名

class,switchString,BooleanV1.3

1:要切换的CSS类名.

2:用于决定元素是否包含class的布尔值。

switchBooleanV1.4

用于决定元素是否包含class的布尔值。

function(index, class,switch)[, switch]Function,BooleanV1.4

1:用来返回在匹配的元素集合中的每个元素上用来切换的样式类名的一个函数。接收元素的索引位置和元素旧的样式类作为参数。

2: 一个用来判断样式类添加还是移除的 boolean 值。

示例

参数class 描述:

为匹配的元素切换 'selected' 类

jQuery 代码:
$("p").toggleClass("selected");

参数class,switch 描述:

每点击三下加上一次 'highlight' 类

HTML 代码:
<strong>jQuery 代码:</strong>
jQuery 代码:
  var count = 0;
  $("p").click(function(){
      $(this).toggleClass("highlight", count++ % 3 == 0);
  });

回调函数 描述:

根据父元素来设置class属性

jQuery 代码:
$('div.foo').toggleClass(function() {
  if ($(this).parent().is('.bar') {
    return 'happy';
  } else {
    return 'sad';
  }
});
阿里云专属限量优惠券,限时领取~