jQuery-删除所有具有相似名称的类 [英] jQuery - Remove all classes with similar names

查看:91
本文介绍了jQuery-删除所有具有相似名称的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有更好的方法吗?

$('element').removeClass('class-1').removeClass('class-2').removeClass('class-3').removeClass('class-5') ...
to .removeClass('class-105')
:)

我要删除所有的class-(n)类.

I want to remove all class-(n) classes.

推荐答案

获取元素的类,将其作为字符串处理,然后放回去:

Get the classes of the element, process it as a string, and put it back:

$('element').attr(
  'className',
  $('element').attr('className').replace(/\bclass-\d+\b/g, '')
);

此后attr方法已更改,不再读取属性,因此您必须使用属性名称class而不是属性名称className:

The attr method has since changed, and it no longer reads properties, so you have to use the attribute name class instead of the property name className:

$('element').attr(
  'class',
  $('element').attr('class').replace(/\bclass-\d+\b/g, '')
);

这篇关于jQuery-删除所有具有相似名称的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆