Javascript 使用jQuery修改类 测试 测试 测试 测试 测试 测试 设置1 设置2 设置3 第4组 $('.set')。单击(函数(){ $(“#这里”)。??? })
我想-如果我点击.set,那么这应该得到自己的类并全部替换。这里 例如,如果我单击集合2,则所有span with class.如下所示:Javascript 使用jQuery修改类 测试 测试 测试 测试 测试 测试 设置1 设置2 设置3 第4组 $('.set')。单击(函数(){ $(“#这里”)。??? }),javascript,jquery,html,regex,Javascript,Jquery,Html,Regex,我想-如果我点击.set,那么这应该得到自己的类并全部替换。这里 例如,如果我单击集合2,则所有span with class.如下所示: <span class="here one-two-del-del other1">test</span> <span class="here one-two-aaa o2ther">test</span> <span class="here one-two-dsf other3">test<
<span class="here one-two-del-del other1">test</span>
<span class="here one-two-aaa o2ther">test</span>
<span class="here one-two-dsf other3">test</span>
<span class="here one-two-213 o4ther">test</span>
<span class="here one-two-sdf other5">test</span>
<span class="here one-two-sdff o6ther">test</span>
<span class="set one-two-aaaa">set 1</span> <br />
<span class="set one-two-bb-fff">set 2</span> <br />
<span class="set one-two-vcvc">set 3</span> <br />
<span class="set one-two-fgdfg-dfgfd-fgf">set 4</span> <br />
$('.set').click(function(){
$('#here').???
})
测试
测试
测试
测试
测试
测试
我不能使用removeClass,因为我不知道当前的类是怎样的。如果我使用.attr('class','new'),那么它将替换所有类,但我必须仍然使用类other1,o2ther等
也许我必须使用正则表达式?HTML
<span class="here one-two-bb-fff other1">test</span>
<span class="here one-two-bb-fff o2ther">test</span>
<span class="here one-two-bb-fff other3">test</span>
<span class="here one-two-bb-fff o4ther">test</span>
<span class="here one-two-bb-fff other5">test</span>
<span class="here one-two-bb-fff o6ther">test</span>
这里有一个问题:有几个类属性会使您的文档无效。您不需要多个“类”属性。没有有效的标记。不能多次使用class属性。
<span class="here one-two-del-del other1">test</span>
<span class="here one-two-aaa o2ther">test</span>
<span class="here one-two-dsf other3">test</span>
<span class="here one-two-213 o4ther">test</span>
<span class="here one-two-sdf other5">test</span>
<span class="here one-two-sdff o6ther">test</span>
<br />
<span class="set one-two-aaaa">set 1</span> <br />
<span class="set one-two-bb-fff">set 2</span> <br />
<span class="set one-two-vcvc">set 3</span> <br />
<span class="set one-two-fgdfg-dfgfd-fgf">set 4</span>
$('.set').click(function () {
var replaceWith = $(this).attr('class');
// Remove the "set " from replaceWith
replaceWith = replaceWith.replace(/set ?/g, '');
$('.here').each(function () {
var newClass = $(this).attr('class');
newClass = newClass.replace(/one-two-[^ $]+/, '');
newClass = newClass + ' ' + replaceWith;
// Now set the new class
$(this).attr('class', newClass);
});
})