Activate the tab of the location hash

后端 未结 1 1952
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 01:06

I have a structure of tabs with 3 levels:


相关标签:
1条回答
  • 2020-12-22 02:01

    That's because #home3-2 is nested within a tab that is hidden.

    Another way to look at this is what would happen for the following code?:

    <div> 1
        <div style="display:none"> 2
            <div style="display:block"> 3 </div>
        </div>
    </div>
    

    Even though you have made the bottom div visible, it will still be hidden by its parent.

    When loading the page, you'll have to traverse the dom for any hidden parent tabs and call show on them as well.

    if (location.hash) {
        $('a[href=' + location.hash + ']').tab('show');
        // code to also show parent tabs
    }
    

    You could do that like this:

    //get the hashed element and find all of it's parent tabs that aren't active
    $(location.hash).parents('.tab-pane:not(.active)').each(function() {
        //each pane should have an id - find it's anchor target and call show
        $('a[href=#' + this.id + ']').tab('show');
    });
    

    Demo in fiddle:

    http://jsfiddle.net/KyleMit/bvta2/11/show/#home3-2

    0 讨论(0)
提交回复
热议问题