jQuery自定义选项卡切换类

时间:2016-01-06 20:41:23

标签: jquery

Sorta得到了这个工作,但试图找出其余部分:

我有2个标签,点击后,应根据课程.is-active更改样式。我首先检查if语句以查看该选项卡是否具有该类,然后如果它发生,则应该单击它时单独使用它。如果它没有该类,那么它应该添加该类并将其从具有它的其他选项卡中删除。

$(function() {
  "use strict";

  // tabbed content
  $('.js-items-tabs .js-tab').click(function(e) {
    e.preventDefault();

    // check if has active class
    if ($(this).hasClass('is-active')) {
      // do nothing
      $(this).toggleClass('is-active');
    } else {
      $(this).toggleClass('is-active');
    }
  });

});
.tabs-container {
  background: white;
  position: relative;
  height: 58px
}
.tabs-container .tabs {
  text-transform: uppercase;
  overflow: hidden
}
.tabs-container .tabs a {
  text-decoration: none;
  font-size: 1.125rem;
  background: #e6e6e6;
  color: #636363;
  display: block;
  float: left;
  padding: 15px 40px;
  width: 40%;
  position: absolute;
  left: 50%;
  box-shadow: inset 1px -2px 10px 0 rgba(0, 0, 0, 0.2)
}
.tabs-container .tabs a.is-active {
  background: white;
  color: black;
  position: absolute;
  top: -10px;
  padding-top: 25px;
  box-shadow: none
}
.tabs-container .tabs a:hover {
  color: black
}
.tabs-container .tabs a.first {
  text-align: right;
  left: 0
}
@media screen and (max-width: 63.9375em) {
  .tabs-container .tabs a {
    font-size: 0.8125rem;
    padding: 15px 10px
  }
}
.tabs-title>a {
  background: #e6e6e6;
  color: #636363
}
.tabs-title>a[aria-selected="true"] {
  padding-top: 2rem
}
.tabs-title>a:hover,
.tabs-title>a:focus,
.tabs-title>a[aria-selected="true"] {
  background: white
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="tabs-container">
  <div class="tabs js-items-tabs" data-tabs id="hiw-tabs">
    <a href="#solar-panels" aria-selected="true" class="js-tab is-active first">Home Solar Panels</a>
    <a href="#getting-started" class="js-tab">Getting Started With Sunnova</a>
  </div>
</section>

2 个答案:

答案 0 :(得分:1)

您需要找到当前活动选项卡,将其从活动状态中删除,然后将活动状态添加到新激活的选项卡中。

$(function() {
  "use strict";

  $('.js-items-tabs .js-tab').click(function(e) {
    e.preventDefault();

    //remove active from currently active
    $('.is-active').removeClass('is-active');
    //add active to this
    $(this).addClass('is-active');
  });
});

如果再次点击活动标签无关紧要,它将丢失,然后立即重新获得活动状态。

答案 1 :(得分:1)

您只是更改了单击的元素的类。您还需要选择同级元素(或者至少选择类.is-active的元素)。

由于元素是兄弟元素,因此您可以将此逻辑简化为单行:

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

如果始终必须至少有一个有效元素,只需将.toggleClass()替换为.addClass()

$(this).addClass('is-active').siblings().removeClass('is-active');

更新示例:

$('.js-items-tabs .js-tab').on('click', function(e) {
  e.preventDefault();
  $(this).toggleClass('is-active').siblings().removeClass('is-active');
});
.is-active {
  color: #f00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="tabs-container">
  <div class="tabs js-items-tabs" data-tabs id="hiw-tabs">
    <a href="#solar-panels" aria-selected="true" class="js-tab is-active first">Home Solar Panels</a>
    <a href="#getting-started" class="js-tab">Getting Started With Sunnova</a>
  </div>
</section>