javascript - jquery removeClass() - Stack Overflow

Is it possible to remove all classes of an element that are not equal to a certain string, for example,

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>

Could I remove all the classes that are not equal to status?

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>

Could I remove all the classes that are not equal to status?

Share Improve this question edited Jan 26, 2011 at 22:37 Šime Vidas 186k65 gold badges288 silver badges391 bronze badges asked Jan 26, 2011 at 22:26 sea_1987sea_1987 2,95412 gold badges46 silver badges69 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742328738a4423280.html

相关推荐

  • javascript - jquery removeClass() - Stack Overflow

    Is it possible to remove all classes of an element that are not equal to a certain string, for example,

    8小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信