2

I've got following code:

<div id="action_wrap">
    <div id="action_nav">
        <div class="action" data-target="#content1" data-toggle="tab">First Block</div>
        <div class="action" data-target="#content2" data-toggle="tab">Second Block</div>
        <div class="action" data-target="#content3" data-toggle="tab">Third Block</div>
    </div>
    <div id="action_items">
        <div class="action_text collapse" id="content1">
            <p>Content 1</p>
            <span class="close">Close</span>
            <div class="clearfix"></div>
        </div>
        <div class="action_text collapse" id="content2">
            <p>Content 2</p>
            <span class="close">Close</span>
            <div class="clearfix"></div>
        </div>
        <div class="action_text collapse" id="content3">
            <p>Content 3</p>
            <span class="close">Close</span>
            <div class="clearfix"></div>
        </div>
    </div>
</div>

and using Twitter Boostrap's "Tab" script, it works fine. I've added some really nasty looking script for close button and for nav button to get active class like so:

$(".action").click(function () {
    $(this).parent().children().removeClass("active");
    $(this).addClass("active");
    $(this).parent().addClass("active");
});

$(".close").click(function () {
    $(this).parent().parent().parent().children().removeClass("active");
    $(this).parent().parent().parent().children().children().removeClass("active");
});

it's not much but meh, it's working.

What i cannot do tho is make Bootstrap's Tab's closable by clicking active nav div. Meaning if i click first .action div with data-target on #content1, .action_text#content1 will get .active class and so will the .action div i clicked on. And what i want is for .action.active data-target="#content1" on click removing .tab()'s class "active" and same for itself.

I found in another thread that changing the code from Bootstrap's Tab script

$(document).on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
    e.preventDefault()
    $(this).tab('show')
})

but it seems everytime i edit it, it just breaks whole thing. I tried adding if statement like so

  $(document).on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
      if ($(this).hasClass('active')) {
        $(this).tab().removeClass('active');
        $(this).removeClass('active');
      } else {
        e.preventDefault()
        $(this).tab('show')
      }
  })

But whole thing stopped working.

4

2 回答 2

2

首先,您应该尽量避免更改 Bootstrap 本机代码,否则您可能会失去升级到未来版本的能力。

关于您的问题,您可能和我一样遇到了一些事件并发问题......

如果您想在单击选项卡切换时关闭活动选项卡,请查看此处: http ://bootply.com/61835 (感谢 katie,我不是此脚本的所有者)

但是,如果您希望能够在以下情况下关闭活动选项卡(切换和窗格):

  • 单击活动选项卡切换
  • 单击选项卡切换或窗格之外的任何其他位置
  • 按下 ESC 键

...那么这个脚本可以帮助你:

$(function() {

    var active = $('a[data-toggle="tab"]').parents('.active').length;
    var tabClicked = false;

    // Closes current active tab (toggle and pane):
    var close = function() {
        $('a[data-toggle="tab"]').parent().removeClass('active');
        $('.tab-pane.active').removeClass('active');
        active = null;
    }

    // Closing active tab when clicking on toggle:
    $('[data-toggle=tab]').click(function(){
        if ($(this).parent().hasClass('active')){
            $($(this).attr("href")).toggleClass('active');
            active = null;
        } else {
            tabClicked = true;
            active = this;
        }
    });

    // Closing active tab when clicking outside tab context (toggle and pane):
    $(document).on('click.bs.tab.data-api', function(event) {
        if(active && !tabClicked && !$(event.target).closest('.tab-pane.active').length) {
            close();
        }

        tabClicked = false;
    });

    // Closing active tab on ESC key release:
    $(document).keyup(function(e){
        if(active && e.keyCode === 27) { // ESC
            close();
        }
    });
});

您可以使用此 JSFiddle 进行尝试:http: //jsfiddle.net/almeidap/EAuUY/(使用 Bootstrap 3)

于 2013-11-30T00:35:51.207 回答
1

即使使用完全限定的 url,这实际上也会关闭标签。

$('[data-toggle=tab]').click(function(){
  if ($(this).parent().hasClass('active')){
    var url = $(this).attr('href');
    var tid = url.split('#')[1];
    console.log(tid);
    $('#'+tid).toggleClass('active');
    document.location.hash = '';
  }
});
 ///  also can work with pills by switching to $('[data-toggle=pill]')
于 2014-03-21T18:37:58.973 回答