难以删除.click()函数上的类

时间:2017-04-28 18:20:28

标签: javascript jquery html

情况:我创建了一个带有点击功能的导航,当用户点击导航链接时,该导航允许子导航到.slideToggle();

问题:点击后,活动类会添加到导航链接,但在用户点击同级导航链接时不会将其自行删除。它继续逐个添加活动类。

问题:如何正确编写函数以将类添加到单击的链接,然后在单击兄弟链接时删除类,同时将类添加到现在选定的链接等。 ..

JS代码:

// Pillar side navigation function
$('.side-nav-link').click(function(e) {
  var $pillarNav = $('.pillar-subnav')

  $(this).removeClass('side-nav-active');

  if ($pillarNav.is(':visible')) {
    $pillarNav.slideUp();
    $(this).next('ul').stop().slideToggle();
  } else {
    $(this).next('ul').stop().slideToggle();
  }

  $(this).addClass('side-nav-active');

  e.preventDefault()
});

以下是工作 JS Fiddle

5 个答案:

答案 0 :(得分:3)

只需从具有该类的所有元素中删除该类,然后将其添加到当前,这是一种更简洁的方法

$('.side-nav-link').click(function(e) {
  e.preventDefault()

  var $pillarNav = $('.pillar-subnav')
  var $subnav = $(this).next('.pillar-subnav').stop().slideToggle();
  var $this = $(this).toggleClass('side-nav-active');

  $('.side-nav-link').not($this).removeClass('side-nav-active');
  $pillarNav.not($subnav).slideUp();
});
.nav {
  padding-left: 0;
  margin-bottom: 0;
  list-style: none;
}

.nav>li {
  position: relative;
  display: block;
}

.nav>li>a {
  position: relative;
  display: block;
  padding: 10px 15px;
}

.pillar-links>li {
  background-color: #f1f1f1;
  border: solid 1px #e3e3e3;
  padding: 5px;
}

.pillar-nav>.side-nav-item>.side-nav-link,
.nav>.side-nav-item>.nav-link {
  color: #555555;
  padding: 15px;
}

.pillar-subnav {
  display: none;
  list-style: none;
  padding: 0;
  background-color: #f1f1f1;
  border-top: 1px solid #d0d0d0;
}

.pillar-subnav li {
  padding: 5px 0;
  border-bottom: 1px solid #d0d0d0;
}

.pillar-subnav li a {
  color: #555555;
  padding: 5px 30px;
  display: inline-block;
}

.pillar-subnav li a:hover {
  text-decoration: none;
  color: #006a65;
  font-weight: bold;
}

.side-heading {
  padding: 5px 15px;
  background-color: #d8d8d8;
  border-bottom: 2px solid #9dc4c3;
  border-radius: 5px 5px 0 0;
}

.side-nav {
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 0 20px #6e9da3;
  width: 220px;
  margin: 20px auto;
}

.side-nav-item {
  position: relative;
  border-bottom: 1px solid #d0d0d0;
}

.side-nav-item::after {
  content: "4";
  font-family: "webdings", sans-serif;
  position: absolute;
  right: 5px;
  top: 25px;
  bottom: 25px;
}

.side-nav-item:last-child::after {
  top: 17px;
}

.side-nav-item:last-child,
.pillar-subnav li:last-child {
  border-bottom: none;
}

.side-nav-item .side-nav-link:hover,
.side-nav-active,
.side-nav-item .nav-link:hover,
.pillar-links>li:hover {
  background-color: #e9e9e9;
  border-left: 4px solid #006a65;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<aside class="side-nav">
  <nav>
    <div class="side-heading">
      <h4>Communication Platform</h4>
    </div>
    <ul class="nav pillar-nav">
      <li class="side-nav-item">
        <a class="side-nav-link" href="#">CSL Behring Transplant Franchise</a>
        <ul class="pillar-subnav">
          <li><a href="#">Transplant Franchise Overview</a></li>
          <li><a href="#">Solid Organ Transplantation Overview</a></li>
        </ul>
      </li>
      <li class="side-nav-item">
        <a class="side-nav-link" href="#">Organ Availability and Patient/Donor Matching</a>
        <ul class="pillar-subnav">
          <li><a href="#">Organ Donation</a></li>
          <li><a href="#">Matching Organs</a></li>
          <li><a href="#">Desensitization</a></li>
        </ul>
      </li>
      <li class="side-nav-item">
        <a class="side-nav-link" href="#">Organ Viability and Donor Management</a>
        <ul class="pillar-subnav">
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
        </ul>
      </li>
      <li class="side-nav-item">
        <a class="side-nav-link" href="#">Ischemia-Reperfusion Injury (IRI)</a>
        <ul class="pillar-subnav">
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
        </ul>
      </li>
      <li class="side-nav-item">
        <a class="side-nav-link" href="#">Transplant Rejection</a>
        <ul class="pillar-subnav">
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
          <li><a href="#">Pillar</a></li>
        </ul>
      </li>
    </ul>
  </nav>
</aside>

答案 1 :(得分:2)

您可以使用not()toggleClass()

$('.side-nav-link').not($(this)).removeClass('side-nav-active');
$(this).toggleClass('side-nav-active');

Demo

答案 2 :(得分:0)

首先从所有内容中删除活动,然后仅应用于$(this)

// Pillar side navigation function
$('.side-nav-link').click(function(e) {
  e.preventDefault()
  var $pillarNav = $('.pillar-subnav')
  $('.side-nav-link').removeClass('side-nav-active');

  $(this).addClass('side-nav-active');

  if ($pillarNav.is(':visible')) {
    $pillarNav.slideUp();
    $(this).next('ul').stop().slideToggle();
  } else {
    $(this).next('ul').stop().slideToggle();
  }
});

答案 3 :(得分:0)

这是一个很好的,可读的:)

<强> jsFiddle demo

// Pillar side navigation function
var $allLinks = $('.side-nav-link');
var $allSubnav = $(".pillar-subnav");

$allLinks.click(function(e) {

  e.preventDefault()

  // LINKS
  $allLinks.not(this).removeClass("side-nav-active"); // All
  $(this).toggleClass("side-nav-active");             // Clicked link

  // SUBNAVS
  var $mySubnav = $(this).closest("li").find(".pillar-subnav");
  $allSubnav.not($mySubnav).stop().slideUp();         // All
  $mySubnav.stop().slideToggle();                     // Related subnav

});

答案 4 :(得分:0)

保持简单,切换$(this)的类并删除兄弟姐妹:

$(this).toggleClass('side-nav-active').siblings().removeClass('side-nav-active');