jQuery - 选项卡内部嵌套选项卡?

3
我可以帮您进行翻译。这是关于编程的内容,需要将标签嵌套在标签内,使用jQuery实现。以下是使用的HTML、CSS和JS代码。请问如何在#tab2中插入一些选项卡?

HTML

<ul class="tabs">
  <li><a href="#tab1">Podcasts</a></li>
  <li><a href="#tab2">Videos</a></li>
</ul>
<div id="tab1" class="tab_content">
  <p>test</p>
</div>
<div id="tab2" class="tab_content">
  <div id="tab2-1">                      <-- Tab 2-1 inside of #tab2
    <p>test</p>
  </div>
  <div id="tab2-2">                      <-- Tab 2-2 inside of #tab2
    <p>test</p>
  </div>
</div>

CSS

.tab_content { background:#fff;padding:10px;width:100% }
.tabs li { display:inline;list-style:none }
.tabs a { background:#7c7c7c;color:#dadada;display:inline-block }
.tabs a.active { background:#e32d2d;color:#fff }

JS

jQuery('ul.tabs').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 = jQuery(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 = jQuery($links.filter('[href="'+location.hash+'"]')[0] || $links[0]);
    $active.addClass('active');

    $content = $($active[0].hash);

    // Hide the remaining content
    $links.not($active).each(function () {
        jQuery(this.hash).hide();
    });

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

        // Update the variables with the new link and content
        $active = jQuery(this);
        $content = jQuery(this.hash);

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

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

1
你需要在#tab2内定义另一个ul,并将其视为另一个选项卡元素。注意不要重复使用id。 - Teh SoTo
1个回答

5

试试这个:

<ul class="tabs">
    <li><a href="#tab1">Podcasts</a></li>
    <li><a href="#tab2">Videos</a></li>
</ul>
<div id="tab1" class="tab_content">
    <p>test</p>
</div>
<div id="tab2" class="tab_content">
    <ul class="tabs">   <!-- Add tabs here -->
        <li><a href="#tab2-1">Tab2-1</a></li>
        <li><a href="#tab2-2">Tab2-2</a></li>
    </ul>
    <div id="tab2-1">
        <p>test 1</p>
    </div>
    <div id="tab2-2">
        <p>test 2</p>
    </div>
</div>

Live Demo


啊,忘了它必须是一个列表。谢谢! - J82
我们如何保留当前子选项卡?我可以使用 'dataStore.getItem' 和 'dataStore.setItem' 来实现主选项卡的操作。但是该逻辑并不适用于子选项卡,每次刷新页面时子选项卡都会默认为默认选项卡。 - Full Stack Developer - Java
@StartCoding,你需要使用setItem来设置选定的标签页,例如setTime('selectedTab','#tab2')。然后在页面加载时,你需要检查是否已经设置了selectedTab,可以使用getItem('selectedTab')进行检查,如果已经设置并且元素存在于页面中(使用.length进行判断),则触发其点击事件,如$(getItem('selectedTab')).trigger('click') - Rohan Kumar
@Rohan Kumar 你能否看一下这个问题 - http://stackoverflow.com/questions/38569190/stay-on-same-tab-upon-refresh - Full Stack Developer - Java
在您的实时演示中,如果我单击Tab2-2上的页面刷新,它不会停留在Tab2-2上。 - Full Stack Developer - Java
亲爱的,我还没有编写代码将当前选定的标签存储在cookieswebstorage中。它只在href中有hash时才起作用。 - Rohan Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接