同时更改2组不同导航链接的活动状态

时间:2016-08-15 16:23:35

标签: jquery html css twitter-bootstrap-3 tabs

我创建了一个左侧导航菜单,其中包含4个控制4个选项卡的导航链接,并且在正文中我复制并粘贴了相同的4个导航链接。

现在,当我点击正文或左侧导航菜单中的导航链接时:所有选项卡都会更改,但由于某种原因,导航链接的活动状态不会更改。

enter image description here

当我点击任意一组链接时,如何让导航链接同时改变活动状态的颜色?

http://fiddle.jshell.net/bym04h3p/1/(拉伸屏幕到769宽度以查看左侧菜单)

//Selection Active 	     
$('.servicelist a').click(function() {
  $('a').removeClass();
  $(this).addClass('active');
});
@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
@import url('https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css');

a {
  outline: none !important;
}
html,
body,
.wrap {
  height: 100%;
}
.wrap {
  box-sizing: border-box;
}
form {
  height: 100%
}
.wrap {
  min-height: 100%;
  height: auto !important;
  height: 100%;
  margin-bottom: -60px;
  /* for sticky footer to not go below page */
  /* for sticky header to not overlap content */
}
.push,
.footer {
  height: 60px;
}
.footer {
  background-color: #ebebeb;
  height: 60px;
  width: 100%;
  position: fixed;
  bottom: 0;
}
.content {
  position: absolute;
  width: 100%;
  top: 120px;
  background-color: yellow;
  z-index: 0;
}
#sidebar-wrapper {
  z-index: 1000;
  position: fixed;
  left: 142px;
  height: 95%;
  width: 0;
  bottom: 0;
  top: 65px;
  height: auto;
  border-top: 1px solid #fff;
  margin-left: -142px;
  overflow-y: auto;
  overflow-x: hidden;
  background: #eee;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}
#sidebar-wrapper2 {
  z-index: 1000;
  position: fixed;
  left: 142px;
  top: 0;
  margin-bottom: 0px;
  width: 100%;
  min-height: 55px;
  height: auto;
  margin-left: -142px;
  background: #eee;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}
#sidebar-wrapper3 {
  z-index: 1000;
  position: fixed;
  left: 142px;
  top: 35px;
  margin-bottom: 0px;
  width: auto;
  min-height: 55px;
  height: auto;
  margin-left: -142px;
  background: #eee;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}
.filtertitle {
  position: relative;
  display: inline-block;
  color: #000;
  width: auto;
}
.topfilter {
  max-width: 100%;
  min-width: 100%;
  position: relative;
  display: inline-block;
  min-height: 65px;
}
.tabber {
  bottom: 0px !important;
  height: auto;
  left: 0;
  position: relative;
  display: inline-block;
  border-bottom: solid #fff 1px;
}
.customtext {
  color: #666;
}
.toppy {
  bottom: 0px !important;
  text-indent: 9px;
}
.bottomfilter {
  border-top: solid #666 1px;
  width: 100%;
  height: auto;
  position: relative;
  display: block;
  text-align: left;
  background-color: #eee;
}
.checkbox {
  margin-left: 25px !important;
}
.dropdown {
  margin-top: 15%;
  display: inline-block;
}
.dropdown a {
  margin-left: 20px;
  min-width: 100% !important;
}
.caret {
  text-align: right !important;
  position: relative;
}
#wrapper.toggled #sidebar-wrapper {
  width: 200px;
}
#wrapper.toggled #sidebar-wrapper2 {
  width: 200px;
}
#page-content-wrapper {
  width: 100%;
  position: absolute;
  padding: 15px;
}
#wrapper.toggled #page-content-wrapper {
  position: absolute;
  margin-right: -200px;
}
@media screen and (min-width: 768px) {
  #wrapper {
    padding-left: 300px;
  }
  #wrapper.toggled {
    padding-left: 0;
  }
  #wrapper.toggled #sidebar-wrapper {
    width: 0;
  }
  #page-content-wrapper {
    padding: 20px;
    position: relative;
  }
  #wrapper.toggled #page-content-wrapper {
    position: relative;
    margin-right: 0;
  }
}
@media screen and (max-width: 1526px) {
  .content,
  .footer {
    width: 82%;
    right: 0;
  }
  .header {
    width: 82%;
  }
  #sidebar-wrapper {
    width: 18%;
  }
  #sidebar-wrapper2 {
    width: 18%;
  }
}
@media screen and (max-width: 1059px) {
  .content,
  .footer {
    width: 80%;
    right: 0;
  }
  .header {
    width: 80%;
  }
  #sidebar-wrapper {
    width: 20%;
  }
  #sidebar-wrapper2 {
    width: 20%;
  }
}
@media screen and (min-width: 1527px) {
  .content,
  .footer {
    width: 85%;
    right: 0;
  }
  .header {
    width: 85%;
  }
  #sidebar-wrapper {
    width: 15%;
  }
  #sidebar-wrapper2 {
    width: 15%;
  }
}
@media screen and (max-width: 991px) {
  .content,
  .footer {
    width: 100%;
    right: 0;
  }
  .filtertitle {
    display: none;
  }
  #sidebar-wrapper {
    width: 0;
  }
  #sidebar-wrapper2 {
    width: 0;
  }
  .topfilter {
    width: 0;
    display: none;
  }
  .dropdown a {
    display: none;
  }
}
/* Service list - Top Sidebar Icon Nav */

.servicelist {
  list-style: none;
  padding: 0;
}
.servicelist a {
  background: none !important;
  text-decoration: none;
  color: #333;
  list-style: none;
}
.servicelist a :hover {
  color: #bdc3c7;
}
.servicelist > li.active > a .fa-2x {
  color: #f90;
  cursor: pointer;
}
<div id="sidebar-wrapper2">

  <!-- Sidebar -->
  <div class="topfilter">

    <div class="tabber">

      <!-- Nav tabs -->
      <ul class="servicelist" role="tablist">

        <li class="col-md-3 col-xs-3 serviceop active" role="presentation">
          <a href="#construction" aria-controls="construction" role="tab" data-toggle="tab">
            <i class="fa-2x fa fa-home"></i>
          </a>
        </li>

        <li class="serviceop col-md-3 col-xs-3" role="presentation">
          <a href="#precon" aria-controls="precon" role="tab" data-toggle="tab">
            <i class="fa-2x fa fa-info-circle"></i> 
          </a>
        </li>

        <li class="col-md-3 col-xs-3 serviceop" role="presentation">
          <a href="#generalcon" aria-controls="generalcon" role="tab" data-toggle="tab">
            <i class="fa-2x fa fa-briefcase"></i> 
          </a>
        </li>

        <li class="col-md-3 col-xs-3 serviceop" role="presentation">
          <a href="#builddesign" aria-controls="builddesign" role="tab" data-toggle="tab">
            <i class="fa-2x fa fa-bullhorn"></i>
          </a>
        </li>

      </ul>

    </div>
    <!-- End Tabber-->
  </div>
  <!-- End Topfilter-->

</div>
<!-- End sidebar-wrapper 2-->


<!-- Tab panes -->
<div class="tab-content">

  <div role="tabpanel" class="tab-pane fade in active" id="construction">
    <br />
    <br />
    <h2>Default</h2>

  </div>
  <!-- End Tab Panel -->

  <div role="tabpanel" class="tab-pane fade" id="precon">

    <br />
    <br />
    <h2>Tab 2</h2>

  </div>
  <!-- End Tab Panel -->



  <div role="tabpanel" class="tab-pane fade" id="generalcon">


    <br />
    <br />
    <h2>Tab 3</h2>

  </div>
  <!-- End Tab Panel -->


  <div role="tabpanel" class="tab-pane fade" id="builddesign">


    <br />
    <br />
    <h2>Tab 4</h2>

  </div>
  <!-- End Tab Panel -->

</div>
<!-- End Tabs Content -->
<div style="float:right;">
  <!-- Nav tabs -->
  <ul class="servicelist" role="tablist">

    <li class="col-md-3 col-xs-3 serviceop active" role="presentation">
      <a href="#construction" aria-controls="construction" role="tab" data-toggle="tab">
        <i class="fa-2x fa fa-home"></i>
      </a>
    </li>

    <li class="serviceop col-md-3 col-xs-3" role="presentation">
      <a href="#precon" aria-controls="precon" role="tab" data-toggle="tab">
        <i class="fa-2x fa fa-info-circle"></i> 
      </a>
    </li>

    <li class="col-md-3 col-xs-3 serviceop" role="presentation">
      <a href="#generalcon" aria-controls="generalcon" role="tab" data-toggle="tab">
        <i class="fa-2x fa fa-briefcase"></i> 
      </a>
    </li>

    <li class="col-md-3 col-xs-3 serviceop" role="presentation">
      <a href="#builddesign" aria-controls="builddesign" role="tab" data-toggle="tab">
        <i class="fa-2x fa fa-bullhorn"></i>
      </a>
    </li>

  </ul>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

2 个答案:

答案 0 :(得分:1)

你想过这个,只需制作一个锚点,显示哪个按钮与哪个按钮一起使用并应用样式。

&#13;
&#13;
$('button').on('click', function() { 
    var setName = $(this).attr('data-set');
    var selector = "button[data-set='"+setName+"']";
  
  
    $('button').removeClass('active'); 
    $(selector).addClass('active');
    
  
});
&#13;
.active {
    background-color: red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button data-set="set1">A</button>
<button data-set="set1">A</button>
<button data-set="set2">B</button>
<button data-set="set2">B</button>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用the shown.bs.tab event代替click事件:

$( '.servicelist a' ).on('shown.bs.tab', function(e){
  if ( !$(this).hasClass( 'active' ) ) {
    $( '.servicelist .active' ).removeClass( 'active' );
    $( '.servicelist a[href="' + $(this).attr( 'href' ) + '"]' ).parent().addClass( 'active' );
  }
});

请检查结果:http://fiddle.jshell.net/glebkema/7Lto4j5a/