简体   繁体   English

引导选项卡中的活动类动态

[英]Active class dynamic in bootstrap tabs

I cant seem to get the active class to change to the corresponding item selected (see code bellow) 我似乎无法让活动班级更改为所选的相应项目(请参见下面的代码)

So I would like the active class to change depending on the thumbnail selected. 因此,我希望根据所选thumbnail来更改活动班级。

html html

 <h2 class="">title</h2>

   <div class="row text-center advice-bar" id="myTab">
        <div class="col-md-3 overlord-thumbnail">
            <div class="thumbnail thumbnail-yellow active">
                <a href="#tab1" data-toggle="tab">
                    <img src="<?php bloginfo('url'); ?>/wp-content/uploads/2017/01/antenna.svg">
                </a>
            </div>
                <h3>one</h3>

                <p>text</p>
        </div>

        <div class="col-md-3 overlord-thumbnail">
            <div class=" thumbnail thumbnail-blue">
                <a href="#tab2" data-toggle="tab">
                    <img src="<?php bloginfo('url'); ?>/wp-content/uploads/2017/01/bar-chart.svg">
                </a>
            </div>
                <h3>two</h3>

                <p>Text</p>
        </div>

        <div class="col-md-3 overlord-thumbnail">
            <div class=" thumbnail thumbnail-red">
                <a href="#tab3" data-toggle="tab">
                    <img src="<?php bloginfo('url'); ?>/wp-content/uploads/2017/01/app.svg">
                </a>
            </div>
                <h3>Three</h3>

                <p>Text</p>
        </div>

        <div class="col-md-3 overlord-thumbnail">
            <div class=" thumbnail thumbnail-green">
                <a href="#tab4" data-toggle="tab">
                    <img src="<?php bloginfo('url'); ?>/wp-content/uploads/2017/01/speech-bubbles.svg">
                </a>
            </div>
                <h3>Foure</h3>

                <p>text</p>
        </div>
    </div>

JavaScript 的JavaScript

$('#myTab a').click(function(e) {
    var $this = $(this);
    $this.parent().siblings().removeClass('active').end().addClass('active');
    e.preventDefault();
});

Try using the following jQuery code: 尝试使用以下jQuery代码:

 $('#myTab a').click(function(e) {
    $($('#myTab a').parent()).addClass("active").not(this.parentNode).removeClass("active");   
    e.preventDefault();
 });

jsfiddle link: https://jsfiddle.net/nashcheez/m1mbp9ke/ jsfiddle链接: https ://jsfiddle.net/nashcheez/m1mbp9ke/

Try removing the active class first using $('.active').removeClass('active'); 尝试首先使用$('.active').removeClass('active');删除active$('.active').removeClass('active'); and change the addClass() to be added like this $this.parent().addClass('active'); 并更改要添加的addClass()这样的$this.parent().addClass('active'); (this will add the active class to the parent of the current clicked element, in this case the element with thumbnail class). (这会将活动类添加到当前单击元素的父级中,在本例中为具有thumbnail类的元素)。 Try this: 尝试这个:

 $('#myTab a').on('click', function(e) { e.preventDefault(); var $this = $(this); $('.active').removeClass('active'); $this.parent().addClass('active'); }); 
 img { width: 100px; } .active { border: 1px solid #F00; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h2 class="">title</h2> <div class="row text-center advice-bar" id="myTab"> <div class="col-md-3 overlord-thumbnail"> <div class="thumbnail thumbnail-yellow active"> <a href="#tab1" data-toggle="tab"> <img src="https://static.pexels.com/photos/1562/italian-landscape-mountains-nature.jpg"> </a> </div> <h3>one</h3> <p>text</p> </div> <div class="col-md-3 overlord-thumbnail"> <div class=" thumbnail thumbnail-blue"> <a href="#tab2" data-toggle="tab"> <img src="https://static.pexels.com/photos/1562/italian-landscape-mountains-nature.jpg"> </a> </div> <h3>two</h3> <p>Text</p> </div> <div class="col-md-3 overlord-thumbnail"> <div class=" thumbnail thumbnail-red"> <a href="#tab3" data-toggle="tab"> <img src="https://static.pexels.com/photos/1562/italian-landscape-mountains-nature.jpg"> </a> </div> <h3>Three</h3> <p>Text</p> </div> <div class="col-md-3 overlord-thumbnail"> <div class=" thumbnail thumbnail-green"> <a href="#tab4" data-toggle="tab"> <img src="https://static.pexels.com/photos/1562/italian-landscape-mountains-nature.jpg"> </a> </div> <h3>Foure</h3> <p>text</p> </div> </div> 

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

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