如何删除以...开头的类? [英] How to remove a class that starts with...?

查看:180
本文介绍了如何删除以...开头的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML

<div class="ativo37 and many other classes"></div>
<div class="another classes here with ativo1"></div>
<div class="here ativo9 and more two or three"></div>

JS

$("div[class^='ativo']").on("click", function(){
    $(this).removeClass("^='ativo'"); // How to achieve it?
});

我可以做什么而不是 .removeClass(^ ='ativo' );

JSFiddle

推荐答案

.removeClass()接受一个函数删除类并接受索引和旧CSS值:

.removeClass() accepts a function for removing classes and accepts index and old CSS value:


返回一个或多个要删除的空格分隔类名的函数。接收set中元素的索引位置和旧类值作为参数。

A function returning one or more space-separated class names to be removed. Receives the index position of the element in the set and the old class value as arguments.

您可以删除以required开头的类名使用以下命名并保留现有:

You can remove the classname that starts with required name and keep the existing using:

$("div[class*='ativo']").removeClass (function (index, css) {
   return (css.match (/(^|\s)ativo\S+/g) || []).join(' ');
});

工作演示

Working Demo

这篇关于如何删除以...开头的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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