我有一个div id="a",可以从几个组中附加任意数量的类.每个组都有一个特定的前缀.在javascript中,我不知道该组中的哪个类在div上.我希望能够清除具有给定前缀的所有类,然后添加一个新类.如果我想删除所有以"bg"开头的类,我该怎么做?像这样的东西,但实际上有效:
$("#a").removeClass("bg*");
Run Code Online (Sandbox Code Playgroud) 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>
Run Code Online (Sandbox Code Playgroud)
JS
$("div[class^='ativo']").on("click", function(){
$(this).removeClass("^='ativo'"); // How to achieve it?
});
Run Code Online (Sandbox Code Playgroud)
我该怎么做而不是.removeClass("^='ativo'");?