开发者

jquery removeClass()

开发者 https://www.devze.com 2023-02-07 10:40 出处:网络
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

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


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

Co开发者_运维百科uld 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


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");

0

精彩评论

暂无评论...
验证码 换一张
取 消