How to make work the jquery nested tab link?

社会主义新天地 提交于 2019-12-11 03:14:52

问题


I am having problem with Jquery tabs UI.

<script type="text/javascript">
$(document).ready(function() {
    $('.tabs').tabs();
    $('.subtabs').tabs();
});

<div class="tabs">
   <ul>
       <li><a href="#tab1">Tab1</a></li>
       <li><a href="#tab2">Tab2</a></li>
   </ul>
       <div id="tab1">
            <div class="subtabs">
                <ul>
                    <li><a href="#subtab1">Subtab1</a></li>
                    <li><a href="#subtab2">Subtab2</a></li>
                </ul>
            <div id="subtab1">
                 Some content1
            </div>
            <div id="subtab2">
                 Some content2
            </div>
            </div>

        </div>
       <div id="tab2"></div>
</div>

Now when i try to access subtab like page.html#subtab1 it does not work, but page.html#tab1 works. What am i doing wrong? basically i need to open subtab using URL.

Thanks


回答1:


You are missing <div id="tab2"></div> which will give a jQuery exception. Try adding that to the code and your links will work.




回答2:


I had the same problem. And I found a JS fix for it. Please note that in your example you had the subtabs in the first tab, the first tab is the default tab that opens. With my fix you can place the subtabs inside any content of the main tab. Check the Plunker for the working example.

http://run.plnkr.co/plunks/Wr91Bm/#subtab2

I created the function openParentTab() and called this right after I created the $('.tabs, .subtabs').tabs();

function openParentTab() {
    locationHash = location.hash.substring( 1 );
    console.log(locationHash);
    // Check if we have an location Hash
    if (locationHash) {
        // Check if the location hash exsist.
        var hash = jQuery('#'+locationHash);
        if (hash.length) {
            // Check of the location Hash is inside a tab.
            if (hash.closest(".tabContent").length) {
                // Grab the index number of the parent tab so we can activate it
                var tabNumber = hash.closest(".tabContent").index();
                jQuery(".tabs.fix").tabs({ active: tabNumber });
            }
        }
    }
}

If you have an big page and you find the focus isnt on the correct subtab you can add the following below jQuery(".tabs.fix").tabs({ active: tabNumber });

hash.get(0).scrollIntoView();
setTimeout(function() {
    hash.get(0).scrollIntoView();
}, 1000);

See code: http://plnkr.co/Wr91Bm



来源:https://stackoverflow.com/questions/10976219/how-to-make-work-the-jquery-nested-tab-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!