一页上有多个选项卡部分



我的网站上的一些响应选项卡使用以下代码:

HTML

div class="vertical-tabs-container">
  <div class="vertical-tabs">
    <a href="javascript:void(0)" class="js-vertical-tab vertical-tab is-active" rel="tab1">Item 1</a>
    <a href="javascript:void(0)" class="js-vertical-tab vertical-tab" rel="tab2">Item 2</a>
    <a href="javascript:void(0)" class="js-vertical-tab vertical-tab" rel="tab3">Item 3</a>
    <a href="javascript:void(0)" class="js-vertical-tab vertical-tab" rel="tab4">Item 4</a>
  </div>
  <div class="vertical-tab-content-container">
    <a href="" class="js-vertical-tab-accordion-heading vertical-tab-accordion-heading is-active" rel="tab1">Item 1</a>
    <div id="tab1" class="js-vertical-tab-content vertical-tab-content">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras tincidunt pellentesque lorem, id suscipit dolor rutrum id. Morbi facilisis porta volutpat. Fusce adipiscing, mauris quis congue tincidunt, sapien purus suscipit odio, quis dictum odio tortor in sem. Ut sit amet libero nec orci mattis fringilla. Praesent eu ipsum in sapien tincidunt molestie sed ut magna. Nam accumsan dui at orci rhoncus pharetra tincidunt elit ullamcorper. Sed ac mauris ipsum. Nullam imperdiet sapien id purus pretium id aliquam mi ullamcorper.</p>
    </div>
    <a href="" class="js-vertical-tab-accordion-heading vertical-tab-accordion-heading" rel="tab2">Item 2</a>
    <div id="tab2" class="js-vertical-tab-content vertical-tab-content">
      <p>Ut laoreet augue et neque pretium non sagittis nibh pulvinar. Etiam ornare tincidunt orci quis ultrices. Pellentesque ac sapien ac purus gravida ullamcorper. Duis rhoncus sodales lacus, vitae adipiscing tellus pharetra sed. Praesent bibendum.</p>
    </div>
    <a href="" class="js-vertical-tab-accordion-heading vertical-tab-accordion-heading" rel="tab3">Item 3</a>
    <div id="tab3" class="js-vertical-tab-content vertical-tab-content">
      <p>Donec mattis mauris gravida metus laoreet non rutrum sem viverra. Aenean nibh libero, viverra vel vestibulum in, porttitor ut sapien. Phasellus tempor lorem id justo ornare tincidunt. Nulla faucibus, purus eu placerat fermentum, velit mi iaculis nunc, bibendum tincidunt ipsum justo eu mauris. Nulla facilisi. Vestibulum vel lectus ac purus tempus suscipit nec sit amet eros. Nullam fringilla, enim eu lobortis dapibus, quam magna tincidunt nibh, sit amet imperdiet dolor justo congue turpis.</p>
    </div>
    <a href="" class="js-vertical-tab-accordion-heading vertical-tab-accordion-heading" rel="tab4">Item 4</a>
    <div id="tab4" class="js-vertical-tab-content vertical-tab-content">
      <p>Sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Phasellus dui urna, mollis vel suscipit in, pharetra at ligula. Pellentesque a est vel est fermentum pellentesque sed sit amet dolor. Nunc in dapibus nibh. Aliquam erat volutpat. Phasellus vel dui sed nibh iaculis convallis id sit amet urna. Proin nec tellus quis justo consequat accumsan. Vivamus turpis enim, auctor eget placerat eget, aliquam ut sapien.</p>
    </div>
  </div>
</div>

SCSS(使用Bourbon和Neat)

.vertical-tabs-container {
  $large-screen: em(640) !default;
  $vertical-tabs-height: em(100);
  @include clearfix;
  .vertical-tabs {
    display: none;
    @include media($large-screen) {
      display: inline;
      float: left;
      height: $vertical-tabs-height;
      width: 20%;
    }
  }
  .vertical-tab {
    @include media($large-screen) {
      display: block;
      &.is-active {
        color: #ff0000;
      }
    }
  }
  .vertical-tab-content-container {
    display: block;
    @include media($large-screen) {
      @include size(80% $vertical-tabs-height);
      display: inline-block;
    }
  }
  .vertical-tab-content {
    @include media($large-screen) {
      display: none;
    }
  }
  .vertical-tab-accordion-heading {
    cursor: pointer;
    display: block;
    @include media($large-screen) {
      display: none;
    }
  }
}

JQUERY

$(".js-vertical-tab-content").hide();
$(".js-vertical-tab-content:first").show();
/* if in tab mode */
$(".js-vertical-tab").click(function(event) {
  event.preventDefault();
  $(".js-vertical-tab-content").hide();
  var activeTab = $(this).attr("rel");
  $("#"+activeTab).show();
  $(".js-vertical-tab").removeClass("is-active");
  $(this).addClass("is-active");
  $(".js-vertical-tab-accordion-heading").removeClass("is-active");
  $(".js-vertical-tab-accordion-heading[rel^='"+activeTab+"']").addClass("is-active");
});
/* if in accordion mode */
$(".js-vertical-tab-accordion-heading").click(function(event) {
  event.preventDefault();
  $(".js-vertical-tab-content").hide();
  var accordionActiveTab = $(this).attr("rel");
  $("#"+accordionActiveTab).show();
  $(".js-vertical-tab-accordion-heading").removeClass("is-active");
  $(this).addClass("is-active");
  $(".js-vertical-tab").removeClass("is-active");
  $(".js-vertical-tab[rel^='"+accordionActiveTab+"']").addClass("is-active");
});

我的问题是,当我的页面上有多个选项卡部分时,它们不是独立工作的。我需要调整javascript,使每个javascript都能独立工作,而不会干扰其他javascript。理想情况下,这将不需要调整HTML。我尝试了几种方法,但都没有成功,所以我回到了这个问题的原始代码。

最好的方法是使用jQuery插件。

但是,对于相同的代码,如果您想轻松解决它,请将所有jQuery代码放在function中,并在发送给该函数的选择器的指导下选择元素。一个示例代码来指导您:

HTML代码

<div id="item1">
    <div class="interactive"></div>
</div>
<div id="item2">
    <div class="interactive"></div>
</div>

jQuery代码

function applyEvent(id){
    var element = $("div#" + id);
    element.find("div.interactive").on("click", function(){
        alert("div inside #" + id + " clicked");
    });
}
//---Apply the events to each div
applyEvent("item1");
applyEvent("item2");

这里有一个jsfiddle,它复制了您的选项卡。

相关内容

  • 没有找到相关文章