如何从类属性中删除与模式匹配的类,但保留其他类?

发布于 2025-01-03 05:05:59 字数 338 浏览 0 评论 0原文

上的类属性中删除以“蓝色”结尾的类

我想从所有标签示例 html

<p class="text_blue happy">this is blue text</p>
<p class="text_red nothappy">this is red text</p>
<img class="img_blue nothappy" />

这将为我提供所有以“蓝色”结尾的类的元素,

$('[class$=blue]');

如何从类属性中弹出这些匹配的类名?

I want to delete the classes that end with 'blue' from the class attribute on all tags

sample html

<p class="text_blue happy">this is blue text</p>
<p class="text_red nothappy">this is red text</p>
<img class="img_blue nothappy" />

This will give me all the elements with classes that end with 'blue'

$('[class$=blue]');

how do I pop these matched classnames off of the class attribute?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2025-01-10 05:05:59

您可以使用如下正则表达式提取整个类名称:

$('[class$="blue"]').each(function() {
    var clsName = this.className.match(/\w*blue\w*/)[0];
});

您应该意识到的一件事是 $('[class$="blue"]') 对名为 < 的整个属性进行操作代码>类。我不对单个类名进行操作。因此,它会匹配:

class="happy text_blue"

但是,它不会匹配:

class="text_blue happy"

因为类属性不以 "blue" 结尾。如果您希望它获取任何包含 "blue" 的类名,无论它在类名属性中的位置如何,您都必须使用:

$('[class*="blue"]').each(function() {
    var clsName = this.className.match(/\w*blue\w*/)[0];
});

如果您还想过滤掉包含以下内容的类名 :不是以蓝色结尾,你可以用 JS 来做到这一点,如下所示:

$('[class*="blue"]').each(function() {
    var match = this.className.match(/\w*blue(\b|$)/);
    if (match) {
        var clsName = match[0];
    }
});

如果你想从对象中删除这些类名,你可以这样做:

$('[class*="blue"]').each(function() {
    var match = this.className.match(/\w*blue(\b|$)/);
    if (match) {
        $(this).removeClass(match[0]);
    }
});

也可以这样做,这看起来更干净,但事实并非如此t 完美地清除要删除的类名周围的额外空格:

$('[class*="blue"]').each(function() {
    this.className = this.className.replace(/\w*blue(\b|$)/, "").replace(/\s+/g, ' ');
});

You can pull the whole class name out with a regular expression like this:

$('[class$="blue"]').each(function() {
    var clsName = this.className.match(/\w*blue\w*/)[0];
});

One thing that you should realize is that $('[class$="blue"]') operates on the entire attribute named class. I does not operate on individual class names. So, it will match:

class="happy text_blue"

But, it will not match:

class="text_blue happy"

because the class attribute does not end with "blue". If you want it to get any class name that contains "blue" regardless of where it is positioned in the class name attribute, you would have to use:

$('[class*="blue"]').each(function() {
    var clsName = this.className.match(/\w*blue\w*/)[0];
});

If you further wanted to filter out class names that did not end with blue, you could do that with JS like this:

$('[class*="blue"]').each(function() {
    var match = this.className.match(/\w*blue(\b|$)/);
    if (match) {
        var clsName = match[0];
    }
});

If you want to remove these class names from the objects, you could do it like this:

$('[class*="blue"]').each(function() {
    var match = this.className.match(/\w*blue(\b|$)/);
    if (match) {
        $(this).removeClass(match[0]);
    }
});

It could also be done this way which seems a little cleaner, but it doesn't perfectly clean up extra whitespace around the class name it's removing:

$('[class*="blue"]').each(function() {
    this.className = this.className.replace(/\w*blue(\b|$)/, "").replace(/\s+/g, ' ');
});
她如夕阳 2025-01-10 05:05:59

如果您要经常这样做,您可能需要更改分配班级的方式。您可以像这样创建 HTML:

<p class="text blue happy">this is blue text</p>

然后在 CSS 中使用 .text.blue 而不是使用 .text_blue 选择器。然后你可以简单地从类中删除“蓝色”。

If you're going to be doing this a lot, you might want to change how you assign classes. You can make your HTML like this:

<p class="text blue happy">this is blue text</p>

and then instead of using a selector of .text_blue in your CSS, use .text.blue instead. Then you can simply remove "blue" from the classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文