jQuery - 从任何
  • 中删除类这不仅仅是点击
  • 发布于 2024-10-16 02:17:28 字数 870 浏览 7 评论 0原文

    可以说我有这样的列表...

    <ul>
      <li class="one">One</li>
      <li class="two">Two</li>
      <li class="three">Three</li>
    </ul>
    

    ...我希望能够在 li 中切换类...

    $( "li" ).click(function() {
        $( "li" ).toggleClass( "active" );
    });
    

    ..而且我希望除了被单击的那个之外的 li 永远不会显示为“.active”仍然保持“.active”li 中的toggleClass 功能

    所以我想也许我想从除单击的那个li 之外的任何其他li 中删除Class。我不知道我怎么能做到这一点......

    关于如何实现类似的事情有什么想法吗?我确信它相当简单,但我不知道,因为我对 jQuery 很陌生

    已解决 - 如果有人想看它..我们就去: http://jsfiddle.net/6jm2s/17/ 还有这个... http://jsfiddle.net/6jm2s/18/

    两个答案都很完美.. @乔达维约翰@hunter 谢谢你们...太糟糕了,我不能在两个答案上都打勾...但我会让猎人拥有它,因为他的代码更短..:)

    Lets say I have list like this...

    <ul>
      <li class="one">One</li>
      <li class="two">Two</li>
      <li class="three">Three</li>
    </ul>
    

    ...and I want to be able to toggle class in the li's...

    $( "li" ).click(function() {
        $( "li" ).toggleClass( "active" );
    });
    

    ..also I want the li's except the one that was clicked to never show as '.active' while still maintaining the toggleClass ability in the '.active' li

    So I was thinking maybe I want to removeClass from any other li's except the one what was clicked. I don't know how I could do that though...

    Any ideas on how to achieve something like that? I'm sure its rather easy but I have no idea as I'm very new to jQuery

    Solved - If someone wants to see it.. there we go:
    http://jsfiddle.net/6jm2s/17/
    also this one...
    http://jsfiddle.net/6jm2s/18/

    Both answers work perfectly.. @jondavidjohn & @hunter Thank you guys... too bad I can't put Checkmark on both answers... but I will let hunter have it as his code is shorter.. :)

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    转角预定愛 2024-10-23 02:17:29

    确保一旦某项处于活动状态,就始终处于活动状态:

    $("li:not(.active)").live("click", function() {
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
    });
    

    仅关注尚未处于活动状态的 li


    能够停用:

    $("li").click(function() {
        $(this).toggleClass("active");
        $(this).siblings().removeClass("active");
    });
    

    工作示例:http://jsfiddle.net/6jm2s/18/

    Make it so that once something is active, one will always be active:

    $("li:not(.active)").live("click", function() {
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
    });
    

    focuses only on li's that aren't already active.


    To be able to deactivate:

    $("li").click(function() {
        $(this).toggleClass("active");
        $(this).siblings().removeClass("active");
    });
    

    working example: http://jsfiddle.net/6jm2s/18/

    汹涌人海 2024-10-23 02:17:29
    $( "li" ).click( function() {
        if( $(this).is('.active') ) {
            $(this).removeClass( "active" );
        }
        else {
            $( "li.active" ).removeClass( "active" );
            $(this).addClass( "active" );
        }
    });
    
    $( "li" ).click( function() {
        if( $(this).is('.active') ) {
            $(this).removeClass( "active" );
        }
        else {
            $( "li.active" ).removeClass( "active" );
            $(this).addClass( "active" );
        }
    });
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文