简体   繁体   English

jQuery removeClass()

[英]jquery removeClass()

Is it possible to remove all classes of an element that are not equal to a certain string, 是否可以删除元素的所有不等于特定字符串的类,

for example, if I have the following HTML 例如,如果我有以下HTML


<a href="" class="status pending"></a>
<a href="" class="status successful"></a>
<a href="" class="status unsuccessful"></a>

Could I remove all the classes that are not equal to status? 我可以删除所有不等于状态的类吗?

You could do this: 您可以这样做:

removeClass().addClass('status') 

Another option: 另外一个选项:

 removeClass(function(i, c) { return c.replace('status', ''); }); 

or a bit faster 或快一点

$('.status').attr('class','status');

will overwrite the class attribute of every element to hold only the 'status' value 将覆盖每个元素的class属性,使其仅保留“ status”值

if ($('a').hasClass("status")) {
   $('a').removeClass().addClass("status") 
} else {
   $('a').removeClass()
}

If you just want the status class to remain on all links you could do: 如果只希望状态类保留在所有链接上,则可以执行以下操作:

$("a").attr("class", "status"); $(“ a”)。attr(“ class”,“ status”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM