Button to Trigger Nav-Tab with Twitter Bootstrap

偶尔善良 提交于 2019-12-03 00:12:46

You could assign your Review button a click handler using jQuery...

JS:

$('#btnReview').click(function(){
  $('.nav-tabs > .active').next('li').find('a').trigger('click');
});

HTML:

<a class="btn btn-primary" href="#" id="btnReview">Review</a>

Working Demo

By changing 'data-toggle' to 'data-trigger' for the triggering link, the following code can trigger any tab with the same HREF attribute, for all triggers with similar markup.

(N.B. Selectors are not optimised, it's just a guide to a more flexible solution)

JS:

$( '[data-trigger="tab"]' ).click( function( e ) {
    var href = $( this ).attr( 'href' );
    e.preventDefault();
    $( '[data-toggle="tab"][href="' + href + '"]' ).trigger( 'click' );
} );

HTML:

<a class="btn btn-primary" href="#tab2" data-trigger="tab">Review</a>

Using Bootstrap 4

var nextTab;    
    $('.nav-link').map(function(element) {
                    if($(this).hasClass("active")) {
                        nextTab = $(this).parent().next('li');
                    }
                })

    nextTab.find('a').trigger('click');

It's quite simple with Bootstrap 4 & JQuery, i'm using this solution:

 $(document).ready(function(){
        activaTab('aaa');
    });

    function activaTab(tab){
        $('.tab-pane a[href="#' + tab + '"]').tab('show');
    };
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!