jQuery - 选项卡内的选项卡?

标签 jquery jquery-tabs

我正在尝试使用 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();
    });
});

最佳答案

试试这个,

<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

关于jQuery - 选项卡内的选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24797405/

相关文章:

javascript - 检查每个输入是否有值(value)

jQuery 选项卡和验证不能一起工作

jquery - Microsoft JScript 运行时错误 : no such method 'select' for tabs widget instance

jquery - 如何制作一个加载页面一次的 jQuery 水平选项卡

javascript - jquery Accordion - 如何在页面加载时默认打开第二个选项卡

javascript - 将一个隐藏的 div 复制到另一个 div

javascript - 没有出现成功

javascript - jQuery SerializeArray 和 Not 选择器

javascript - 插入 jquery 对象数组,但仅插入一个对象

jquery - 需要 jQuery 选项卡当前选定的选项卡 ID