jquery - 如何在 css 列表中使用 jquery 交换类/id?

标签 jquery css class html-lists swap

我已经设置了以下 fiddle :

http://jsfiddle.net/Rb2Pz/4/

基本上,我试图在单击时在 li 上交换类(即删除类电机,然后添加所选类。

此外,如果我希望使用 Id 而不是类,我应该使用什么代码?

提前致谢。

最佳答案

已更新 here .

$(function() {
    $('a').click(function() {
       $(this).parents('.motor').removeClass('motor').addClass('selected');
    });
});

如果您想使用 id 而不是类(在这种情况下您可能不应该这样做),您可以将 ('.motor') 切换为 ('#motor') removeClass('motor')removeAttr('id'),然后 addClass('selected')attr('id','selected').同样,在这种情况下,类更好。

关于jquery - 如何在 css 列表中使用 jquery 交换类/id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7788667/

相关文章:

javascript - jQuery 的 .text() 和 JavaScript 的 .innerText 都不起作用?

jquery 数据表 : fetch next 25 rows from databse on next page button of jQuery datatable

javascript - 最好的 jQuery 状态消息插件?

html - 并排放置三个按钮不会在顶部放置一个大按钮

html - CSS - 选定的链接不保持颜色

javascript - jQuery 复选框样式问题

jquery - CSS 不适用于 jQuery mobile 中动态创建的元素

ios - 如何将我的 View 之一与 UIView 类连接?

java - 如何有选择地重写类的函数

c++ - 将一个对象中的数组设置为等于另一个对象中的数组?