使用jQuery更改具有默认值的链接类 [英] Change class of links with default values with jQuery

查看:50
本文介绍了使用jQuery更改具有默认值的链接类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个手动设置类的链接:一个默认为 .active ,其他默认为 .inactive 。我有一些jQuery,我用它来切换链接的类,当它们被点击为CSS目的。这适用于 .inactive 链接 - 它们切换到 .active 。但是,它不适用于默认的 .active 链接。如果我点击默认的 .active 链接,它将保持不活动状态。

I have three links on which I manually set classes: One defaults to .active and the others default to .inactive. I have some jQuery that I'm using to toggle the class of the links when they are clicked for CSS purposes. This works for the .inactive links - they switch to .active. However it doesn't work for the default .active link. If I click on the default .active link it stays inactive.

默认加载:

链接1 - 活跃
链接2 - 无效
链接3 - 无效

Link 1 - Active Link 2 - Inactive Link 3 - Inactive

点击链接2:

链接1 - 无效
链接2 - 有效
链接3 - 无效

Link 1 - Inactive Link 2 - Active Link 3 - Inactive

点击链接2中的链接1:

Clicking Link 1 from Link 2:

链接1 - 无效
链接2 - 有效
链接3 - 无效

Link 1 - Inactive Link 2 - Active Link 3 - Inactive

如何完成此循环以使其有效?

How can I complete this cycle so it works?

以下是我正在生成的HTML:

<ul id="infoContainer">
  <li><a href="/profiles/1/profile_cred" class="active" data-remote="true">Cred</a></li>
  <li><a href="/profiles/1/profile_about" class="inactive" data-remote="true">About</a></li>
  <li><a href="/profiles/1/profile_questions" class="inactive" data-remote="true">Questions</a></li>
</ul>

我正在使用的jQuery:

$(function(){
    var sidebar=$('#sidebar');
    sidebar.delegate('a.inactive', 'click',function(){
        sidebar.find('.active').toggleClass('active inactive');
        $(this).toggleClass('active inactive');
    });
});


推荐答案

如果我确实告诉你,你的选择器也是strict:仅处理无效链接。

试过以下内容?

If I do get you correct, your selector is too strict: handling only the inactive links.
Tried the following?

$(function(){
    var sidebar=$('#sidebar');
    sidebar.delegate('a', 'click',function(){
        sidebar.find('.active').toggleClass('active inactive');
        $(this).addClass('active').removeClass('inactive');
    });
});

这篇关于使用jQuery更改具有默认值的链接类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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