2

我在将下拉菜单与选项卡链接时遇到问题。我想要的是,在使用选项卡并通过下拉菜单或 Bootstrap 3 中的选项卡导航获取内容时,除了内容(该类称为“活动”)之外,相关选项卡还可以切换为“活动” .

这是控制选项卡的 boostrap.js。

  Tab.prototype.show = function () {
    var $this    = this.element
    var $ul      = $this.closest('ul:not(.dropdown-menu)')
    var selector = $this.attr('data-target')

    if (!selector) {
      selector = $this.attr('href')
      selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
    }

    if ($this.parent('li').hasClass('active')) return

    var previous = $ul.find('.active:last a')[0]
    var e        = $.Event('show.bs.tab', {
      relatedTarget: previous
    })

    $this.trigger(e)

    if (e.isDefaultPrevented()) return

    var $target = $(selector)

    this.activate($this.parent('li'), $ul)
    this.activate($target, $target.parent(), function () {
      $this.trigger({
        type: 'shown.bs.tab'
      , relatedTarget: previous
      })
    })
  }

  Tab.prototype.activate = function (element, container, callback) {
    var $active    = container.find('> .active')
    var transition = callback
      && $.support.transition
      && $active.hasClass('fade')

    function next() {
      $active
        .removeClass('active')
        .find('> .dropdown-menu > .active')
        .removeClass('active')

      element.addClass('active')

      if (transition) {
        element[0].offsetWidth // reflow for transition
        element.addClass('in')
      } else {
        element.removeClass('fade')
      }

      if (element.parent('.dropdown-menu')) {
        element.closest('li.dropdown').addClass('active')
      }

      callback && callback()
    }

    transition ?
      $active
        .one($.support.transition.end, next)
        .emulateTransitionEnd(150) :
      next()

    $active.removeClass('in')
  }

这是我的代码(它可以很好地在选项卡中显示相关内容,但选项卡导航没有切换“活动”类:

<div class="navbar navbar-fixed-top">
  <div class="container">
    <div class="row">
      <a class="navbar-brand" href="#"><img class="img-responsive" src="img/logo.jpg"></a>        
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <div class="nav-collapse collapse">
        <ul class="nav navbar-nav">
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Skin Therapies <b class="caret"></b></a>
            <ul class="dropdown-menu" id="treatment_dropdown">
              <li><a href="#acne_and_skin_health" id="acne_and_skin_health_li">Skin Health</a></li>
              <li><a href="#pigmentation_and_brightening" id="pigmentation_and_brightening_li">Pigmentation &amp; Brightening</a></li>
              <li><a href="#anti_aging" id="anti_aging_li">Anti-Aging</a></li>
              <li><a href="#pregnancy" id="pregnancy_li">Pregnancy Stretchmarks &amp; Melasma</a></li>
              <li><a href="#scars" id="scars_li">Scars</a></li>
              <li><a href="#immune_compromised_skin" id="immune_compromised_skin_li">Post Chemo &amp; Radiation Therapy</a></li>
              <li><a href="#fat_and_cellulite" id="fat_and_cellulite_li">Fat &amp; Cellulite Reduction</a></li>
              <li><a href="#vascular" id="vascular_li">Rosacea, Angiomas &amp; Spider Veins</a></li>
              <li><a href="#laser_hair_removal" id="laser_hair_removal_li">Laser Hair Removal</a></li>
              <li><a href="#">Special Events</a></li>
              <li><a href="#">For our Male Clients</a></li>                
            </ul>
          </li>
        </ul>
      </div>

然后我有我的标签....

      <!-- Treatment Menu -->
  <div class="row">
    <ul class="nav nav-tabs" id="treatment_tabs">
      <li><a href="#acne_and_skin_health" id="acne_and_skin_health_tt">Skin Health</a></li>
      <li><a href="#pigmentation_and_brightening" id="pigmentation_and_brightening_tt">Pigmentation &amp; Brightening</a></li>
      <li><a href="#anti_aging" id="anti_aging_tt">Anti-Aging</a></li>
      <li><a href="#pregnancy" id="pregnancy_tt">Pregnancy</a></li>
      <li><a href="#scars" id="scars_tt">Scars</a></li>
      <li><a href="#immune_compromised_skin" id="immune_compromised_skin_tt">Immune Compromised Skin</a></li>
      <li><a href="#fat_and_cellulite" id="fat_and_cellulite_tt">Fat &amp; Cellulite</a></li>
      <li><a href="#vascular" id="vascular_tt">Vascular</a></li>
      <li><a href="#laser_hair_removal" id="laser_hair_removal_tt">Laser Hair Removal</a></li>
    </ul>

    <div class="tab-content">
      <div class="tab-pane fade active" id="acne_and_skin_health">
        ...
      </div>
      <div class="tab-pane fade" id="pigmentation_and_brightening">
        ...
      </div>
      <div class="tab-pane fade" id="anti_aging">
        ...
      </div>
      <div class="tab-pane fade" id="pregnancy">
        ...
      </div>
      <div class="tab-pane fade" id="scars">
        ...
      </div>
      <div class="tab-pane fade" id="immune_compromised_skin">
        ...
      </div>
      <div class="tab-pane fade" id="fat_and_cellulite">
        ...
      </div>
      <div class="tab-pane fade" id="vascular">
        ...
      </div>
      <div class="tab-pane fade" id="laser_hair_removal">
        ...
      </div>
    </div>        
  </div>
</div>

这是相关的 javascript 调用:

    <script>
  $('#treatment_tabs a:first').tab('show') // Select first tab
  $('#treatment_tabs a').click(function (e) {
    e.preventDefault()
    $(this).tab('show')
  })
  $('#treatment_dropdown a').click(function (e) {
    e.preventDefault()        
    $(this).tab('show')
  })
</script>
4

1 回答 1

3

可以标签的元素是 a (锚)而不是内容。因此,单击下拉列表中的链接应该会激活选项卡栏锚点上的选项卡():

  $('#treatment_dropdown a').click(function (e) {
    e.preventDefault()        
    $($(this).attr('href')+'_tt').tab('show')
  })
于 2013-08-22T18:39:15.480 回答