返回值:jQueryremoveClass([className])

移除每个匹配元素上的一个,多个或所有样式。

如果将一个样式名作为参数,该方法会移除匹配元素对应的样式。如果不指定样式名,则所有样式都会被删除。

一次可以移除多个样式,样式之间用空格分隔。例如:

$('p').removeClass('myClass yourClass')

该方法经常与 .addClass() 一起使用,用来改变元素的样式。例如:

$('p').removeClass('myClass noClass').addClass('yourClass');

上例中,所有段落中的 myClassnoClass 样式都会被移除,然后添加 yourClass 样式。

若要替换所有已经存在的样式,可以使用 .attr('class', 'newClass') 来代替。

从 jQuery 1.4 开始,.removeClass() 方法允许我们指定一个函数作为参数,返回将要被删除的样式。

$('li:last').removeClass(function() {
          return $(this).prev().attr('class');
        });

上例中,移除了最后一个 <li> 中的如下样式:该样式是倒数第二个 <li> 的样式。

示例:

移除匹配元素上的 'blue' 样式。

<!DOCTYPE html>
<html>
<head>
<style>

  p { margin: 4px; font-size:16px; font-weight:bolder; }
  .blue { color:blue; }
  .under { text-decoration:underline; }
  .highlight { background:yellow; }
  </style>
<script src="jquery.min.js"></script>
</head>
<body>

<p class="blue under">Hello</p>
  <p class="blue under highlight">and</p>
  <p class="blue under">then</p>

  <p class="blue under">Goodbye</p>

<script>

$("p:even").removeClass("blue");

</script>
</body>
</html>

演示:

示例:

移除匹配元素上的 'blue' 和 'under' 样式。

<!DOCTYPE html>
<html>
<head>
<style>
  p { margin: 4px; font-size:16px; font-weight:bolder; }
  .blue { color:blue; }
  .under { text-decoration:underline; }
  .highlight { background:yellow; }
  </style>
<script src="jquery.min.js"></script>
</head>
<body>

<p class="blue under">Hello</p>

  <p class="blue under highlight">and</p>
  <p class="blue under">then</p>
  <p class="blue under">Goodbye</p>

<script>

$("p:odd").removeClass("blue under");

</script>
</body>
</html>

演示:

示例:

移除匹配元素上的所有样式。

<!DOCTYPE html>
<html>
<head>
<style>

  p { margin: 4px; font-size:16px; font-weight:bolder; }
  .blue { color:blue; }
  .under { text-decoration:underline; }
  .highlight { background:yellow; }
  </style>
<script src="jquery.min.js"></script>
</head>
<body>

<p class="blue under">Hello</p>
  <p class="blue under highlight">and</p>
  <p class="blue under">then</p>

  <p class="blue under">Goodbye</p>

<script>

$("p:eq(1)").removeClass();

</script>
</body>
</html>

演示: