使用 jQuery 修改类
Modify class with jQuery
<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').???
})
我想要 - 如果我点击 .set,那么它应该得到自己的类并在所有 .here 中替换。
例如,如果我单击集合 2,那么所有带有类 .here 的跨度都应该是:
<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>
我无法使用 removeClass 因为我不知道当前类如何。如果我使用 .attr('class', 'new'(,那么这将替换所有类,但我仍然必须有类 other1、o2ther 等
http://jsfiddle.net/NNhDd/
也许我必须使用正则表达式?
HTML
<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>
JavaScript
$('.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);
});
})
这里有一个小提琴:http://jsfiddle.net/NNhDd/3/
相关文章:
- 修改jQuery脚本,让我选择页面入口的选择列表的默认值
- 有没有一种方法可以修改jQuery对象本身
- 修改 jQuery 和 PHP 代码,将 ID 和文本值拉取到 OPTION 元素中
- 需要修改 jQuery
- 修改 JQuery 对象
- 修改 jQuery 自动完成,不要在 Enter 上急切提交
- 正在修改jQuery验证消息
- 修改jQuery进度条's基于Gridview列的颜色
- 修改jQuery javascript以触发:checked而不是:hover--image swap
- 如何在ASP.net网格视图中修改jQuery进度条颜色
- 如何动态修改jQuery多选下拉菜单的背景颜色
- 修改Jquery以展开不同位置的每张照片
- 修改jquery以在单击时不打开图像
- 修改 jquery $.get 函数响应
- 修改jQuery$函数以支持遗留代码
- 修改jQuery滑块以获得更大的控制:添加可点击的预览缩略图,该缩略图指向相应的幻灯片
- 如何修改jquery对话框按钮
- 修改jquery脚本来使用它.parent在appendTo()函数中
- 如何:修改jquery.自动补全正则表达式以显示从给定输入开始的结果
- 修改Jquery. fancybox-1.3.4.js文件,重写_draw函数