简体   繁体   English

Bootstrap选项卡式导航故障

[英]Bootstrap Tabbed Navigation glitch

I am having some issues with a tabbed block on my website. 我的网站上的标签块存在一些问题。 I have a demo link at the foot of this. 我在此脚下有一个演示链接。

Firstly when I click the tabs, they don't remain active after clicking. 首先,当我单击选项卡时,它们在单击后不会保持活动状态。 Also, I want to only show the tab content that has been clicked. 另外,我只想显示已单击的选项卡内容。 Right now, it just lists all 5 divs. 现在,它仅列出所有5个div。 Help? 救命?

HTML: HTML:

JS: JS:

$('ul.tabs li').click(function() {
        var tab_id = $(this).attr('data-tab');

        $('ul.tabs li').removeClass('current');
        $('.tab-content').removeClass('current');

        $(this).addClass('current');
            $("#"+tab_id).addClass('current');
        });

Demo: https://jsfiddle.net/czh8q2Le/ 演示: https : //jsfiddle.net/czh8q2Le/

You could add a new class to your SCSS called .show-tab 您可以在SCSS中添加一个名为.show-tab的新类。

.show-tab {
  display: block;
}

Also, you will want to add the following attribute to your .tab-content 另外,您将需要在.tab-content添加以下属性

.tab-content {
  display: none;
}

This allows all your divs to be initially invisible, and then make them visible once you add the .show-tab class to the div. 这样可以使所有div最初不可见,然后在将.show-tab类添加到div后使其可见。

Then in your HTML replace your current tag for your divs with show-tab like so (this makes it so the first div is initially shown) 然后在您的HTML中,用show-tab替换div的当前标签,如下所示(这样可以使第一个div最初显示)

<div class="tab-content show-tab" id="tab-pencil-pleat">
    1
</div>
<div class="tab-content" id="tab-delivery">
    2
</div>
<div class="tab-content" id="tab-eyelet">
    3
</div>
<div class="tab-content" id="tab-tabtop">
    4
</div>
<div class="tab-content" id="tab-wave">
    5
</div>

Finally, simply change your javascript so it changes the show-tab class, allowing you to add .show-tab to the divs you want to be shown and to remove .show-tab from the divs you don't wish to display: 最后,只需更改您的JavaScript,使其更改show-tab类,即可将.show-tab添加到要显示的div中,并从不希望显示的div中删除.show-tab

$('ul.tabs li').click(function() {
  var tab_id = $(this).attr('data-tab');

  $('ul.tabs li').removeClass('current');
  $('.tab-content').removeClass('show-tab');

  $(this).addClass('current');
  $("#"+tab_id).addClass('show-tab');
});

Hope this helps. 希望这可以帮助。 Here is a working fiddle: https://jsfiddle.net/czh8q2Le/1/ 这是一个工作的小提琴: https : //jsfiddle.net/czh8q2Le/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM