如何修改jQuery选项卡脚本以使用嵌套选项卡?

时间:2012-11-21 02:10:44

标签: jquery

  

可能重复:
  Nested jQuery tabs

我有这段代码:http://jsfiddle.net/ynts/C5FvD/

HTML:

<ul class='tabs'>
    <li><a href='#tab1'>Tab 1</a></li>
    <li><a href='#tab2'>Tab 2</a></li>
    <li><a href='#tab3'>Tab 3</a></li>
</ul>
<hr>
<div id='tab1'>
    <p>Hi, this is the first tab.</p>
</div>
<div id='tab2'>
    <p>This is the 2nd tab.</p>
    <ul class='tabs2'>
        <li><a href='#tab2-1'>Tab 2 → 1</a></li>
        <li><a href='#tab2-2'>Tab 2 → 2</a></li>
        <li><a href='#tab2-3'>Tab 2 → 3</a></li>
    </ul>
    <div id='tab2-1'>
        <p>This is Tab 2 → <strong>1</strong>.</p>
    </div>
    <div id='tab2-2'>
        <p>This is Tab 2 → <strong>2</strong>.</p>
    </div>
    <div id='tab2-3'>
        <p>This is Tab 2 → <strong>2</strong>.</p>
        <ul class='tabs2-3'>
            <li><a href='#tab2-3-1'>Tab 3 → 3 → 1</a></li>
            <li><a href='#tab2-3-2'>Tab 3 → 3 → 2</a></li>
        </ul>
        <div id='tab2-3-1'>
            <p>This is Tab 2 → 3 → <strong>1</strong>.</p>
        </div>
        <div id='tab2-3-2'>
            <p>This is Tab 2 → 3 → <strong>2</strong>.</p>
        </div>
    </div>
</div>
<div id='tab3'>
    <p>And this is the 3rd tab.</p>
</div>​

和此脚本:

$.fn.myTabs = function() {

    return this.each(function() {

        // For each set of tabs, we want to keep track of
        // which tab is active and it's associated content
        var $active, $content, $links = $(this).find('a');

        // If the location.hash matches one of the links, use that as the active tab.
        // If no match is found, use the first link as the initial active tab.
        $active = $($links.filter('[href="'+location.hash+'"]')[0] || $links[0]);
        $active.parent('li').addClass('active');
        $content = $($active.attr('href'));


        // Hide the remaining content
        $links.not($active).each(function () {
                $($(this).attr('href')).hide();
        });

        // Bind the click event handler
        $(this).on('click', 'a', function(e){
                // Make the old tab inactive.
                $active.parent('li').removeClass('active');
                $content.hide();


                // Update the variables with the new link and content
                $active = $(this);
                $content = $($(this).attr('href'));

                // Make the tab active.
                $active.parent('li').addClass('active');
                $content.show();


                // Prevent the anchor's default click action
                e.preventDefault();
        });
    });

};    

$('.tabs, .tabs2, .tabs2-3').myTabs();

这适用于点击,但当我尝试转到嵌套标签http://fiddle.jshell.net/ynts/C5FvD/show/#tab2-3-2的超链接时,会打开第一个顶部标签。我想编辑我的代码以启用嵌套选项卡的链接,但我不能。

©我使用了本教程中的标签脚本代码:jacklmoore.com/notes/jquery-tabs

0 个答案:

没有答案