代码之家  ›  专栏  ›  技术社区  ›  eozzy

管理活动状态

  •  6
  • eozzy  · 技术社区  · 15 年前
    <ul>
        <li><a href="#">item 1</a></li>
        <li><a href="#">item 2</a></li>
        <li><a href="#">item 3</a></li>
    </ul>
    

    单击时,我想添加类 active 到父li元素,同时删除 积极的 从任何其他可能已处于活动状态的元素初始化。

    2 回复  |  直到 10 年前
        1
  •  6
  •   Tomalak    15 年前
    $("li a").click( function() {
      $(".active").removeClass("active");
      $(this).parent("li").addClass("active");
    });
    
        2
  •  2
  •   Matt user129975    15 年前
    $('div.filter').delegate('a', 'click', function (event) {
      var theLi = $(this).closest('li');
    
      theLi.siblings('.active:first').removeClass('active');
      theLi.addClass('active');
    
      $('ul.items li').hide().filter('.' + this.href.slice(this.href.indexOf("#") + 1)).show();
    
      event.preventDefault();
    });