5.6 Bootstrap tab page (Tab) plug-in


Bootstrap tab page (Tab) plugin

insert image description here

Tabs were introduced in the Bootstrap Navigation Elements chapter. By combining some data attributes, you can easily create a tabbed interface. With this plugin you can place content in tabs or capsule tabs or even dropdown menu tabs.

Note: If you want to reference the functionality of the plugin separately, you need to reference tab.js. Alternatively, as mentioned in the Bootstrap Plugins Overview chapter, you can reference bootstrap.js or the minified version of bootstrap.min.js.

usage

You can enable tabs in two ways:

  • Via the data attribute: You need to add data-toggle="tab" or data-toggle="pill" to the anchor text link.

    Adding nav and nav-tabs classes to ul will apply Bootstrap tab styles, adding nav and nav-pills classes to ul will apply Bootstrap capsule styles.

    <ul class="nav nav-tabs">
        <li><a href="#identifier" data-toggle="tab">Home</a></li>
        ...
    </ul>
    
  • Via JavaScript: You can use Javscript to enable tabs as follows:

    $('#myTab a').click(function (e) {
      e.preventDefault()
      $(this).tab('show')
    })
    
  • The following examples demonstrate different ways to activate individual tabs:

    // 通过名称选取标签页
    $('#myTab a[href="#profile"]').tab('show')
     
    // 选取第一个标签页
    $('#myTab a:first').tab('show')
     
    // 选取最后一个标签页
    $('#myTab a:last').tab('show')
     
    // 选取第三个标签页(从 0 开始索引)
    $('#myTab li:eq(2) a').tab('show')
    

fade effect

If you need to set the fading effect for the tab page, please add .fade to the end of each .tab-pane. The first tab must have the .in class added to fade in the initial content, as shown in the example below:

<div class="tab-content">
    <div class="tab-pane fade in active" id="home">...</div>
    <div class="tab-pane fade" id="svn">...</div>
    <div class="tab-pane fade" id="ios">...</div>
    <div class="tab-pane fade" id="java">...</div>
</div>

example

The following example demonstrates the Tab plugin using the data attribute and its fading effect:

<ul id="myTab" class="nav nav-tabs">
    <li class="active">
        <a href="#home" data-toggle="tab">
            CSDN
        </a>
    </li>
    <li><a href="#ios" data-toggle="tab">iOS</a></li>
    <li class="dropdown">
        <a href="#" id="myTabDrop1" class="dropdown-toggle"
           data-toggle="dropdown">Java
            <b class="caret"></b>
        </a>
        <ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
            <li><a href="#jmeter" tabindex="-1" data-toggle="tab">jmeter</a></li>
            <li><a href="#ejb" tabindex="-1" data-toggle="tab">ejb</a></li>
        </ul>
    </li>
</ul>
<div id="myTabContent" class="tab-content">
    <div class="tab-pane fade in active" id="home">
        <p>CSDN是一个中国专业IT社区CSDN (Chinese Software Developer Network) 创立于1999年,致力于为中国软件开发者提供知识传播、在线学习、职业发展等全生命周期服务。</p>
    </div>
    <div class="tab-pane fade" id="ios">
        <p>iOS 是一个由苹果公司开发和发布的手机操作系统。最初是于 2007 年首次发布 iPhone、iPod Touch 和 Apple
            TV。iOS 派生自 OS X,它们共享 Darwin 基础。OS X 操作系统是用在苹果电脑上,iOS 是苹果的移动版本。</p>
    </div>
    <div class="tab-pane fade" id="jmeter">
        <p>jMeter 是一款开源的测试软件。它是 100% 纯 Java 应用程序,用于负载和性能测试。</p>
    </div>
    <div class="tab-pane fade" id="ejb">
        <p>Enterprise Java Beans(EJB)是一个创建高度可扩展性和强大企业级应用程序的开发架构,部署在兼容应用程序服务器(比如 JBOSS、Web Logic 等)的 J2EE 上。
        </p>
    </div>
</div>

The result looks like this:
insert image description here

method

.$().tab: This method can activate tab page elements and content containers. Tab pages need to use a data-target or an href pointing to the container node in the DOM.

<ul class="nav nav-tabs" id="myTab">
    <li class="active"><a href="#identifier" data-toggle="tab">Home</a></li>
    .....
</ul>
<div class="tab-content">
    <div class="tab-pane active" id="home">...</div>
    .....
</div>
<script>
    $(function () {
      
      
        $('#myTab a:last').tab('show')
    })
</script>

insert image description here

example

The following example demonstrates the usage of the Tab plug-in method .tab. In this example, the second tab, iOS, is active:

<ul id="myTab" class="nav nav-tabs">
    <li class="active">
        <a href="#home" data-toggle="tab">CSDN</a>
    </li>
    <li><a href="#ios" data-toggle="tab">iOS</a></li>
    <li class="dropdown">
        <a href="#" id="myTabDrop1" class="dropdown-toggle"
           data-toggle="dropdown">Java <b class="caret"></b>
        </a>
        <ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
            <li><a href="#jmeter" tabindex="-1" data-toggle="tab">
                    jmeter</a>
            </li>
            <li><a href="#ejb" tabindex="-1" data-toggle="tab">
                    ejb</a>
            </li>
        </ul>
    </li>
</ul>
<div id="myTabContent" class="tab-content">
    <div class="tab-pane fade in active" id="home">
        <p>CSDN是一个中国专业IT社区CSDN (Chinese Software Developer Network) 创立于1999年,致力于为中国软件开发者提供知识传播、在线学习、职业发展等全生命周期服务。</p>
    </div>
    <div class="tab-pane fade" id="ios">
        <p>iOS 是一个由苹果公司开发和发布的手机操作系统。最初是于 2007 年首次发布 iPhone、iPod Touch 和 Apple
            TV。iOS 派生自 OS X,它们共享 Darwin 基础。OS X 操作系统是用在苹果电脑上,iOS 是苹果的移动版本。</p>
    </div>
    <div class="tab-pane fade" id="jmeter">
        <p>jMeter 是一款开源的测试软件。它是 100% 纯 Java 应用程序,用于负载和性能测试。</p>
    </div>
    <div class="tab-pane fade" id="ejb">
        <p>Enterprise Java Beans(EJB)是一个创建高度可扩展性和强大企业级应用程序的开发架构,部署在兼容应用程序服务器(比如 JBOSS、Web Logic 等)的 J2EE 上。
        </p>
    </div>
</div>
<script>
    $(function () {
      
      
        $('#myTab li:eq(1) a').tab('show');
    });
</script>

The result looks like this:
insert image description here

event

The following table lists the events to be used in the Tab plugin. These events can be used as hooks in functions.

event describe example
show.bs.tab This event fires when the tab is shown, but before the new tab is shown. Use event.target and event.relatedTarget to target the active tab and the previous active tab, respectively. $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
e.target // 激活的标签页
e.relatedTarget // 前一个激活的标签页
})
shown.bs.tab This event fires when a tab is displayed, but only after a tab has been displayed. Use event.target and event.relatedTarget to target the active tab and the previous active tab, respectively. $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
e.target // 激活的标签页
e.relatedTarget // 前一个激活的标签页
})

example

The following example demonstrates the usage of tab page (Tab) plug-in event. In this example, the current and previously visited tabs are displayed:

<hr>
<p class="active-tab"><strong>激活的标签页</strong><span></span></p>
<p class="previous-tab"><strong>前一个激活的标签页</strong><span></span></p>
<hr>
<ul id="myTab" class="nav nav-tabs">
    <li class="active"><a href="#home" data-toggle="tab">CSDN</a></li>
    <li><a href="#ios" data-toggle="tab">iOS</a></li>
    <li class="dropdown">
        <a href="#" id="myTabDrop1" class="dropdown-toggle"
           data-toggle="dropdown">
            Java <b class="caret"></b></a>
        <ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
            <li><a href="#jmeter" tabindex="-1" data-toggle="tab">jmeter</a></li>
            <li><a href="#ejb" tabindex="-1" data-toggle="tab">ejb</a></li>
        </ul>
    </li>
</ul>
<div id="myTabContent" class="tab-content">
    <div class="tab-pane fade in active" id="home">
        <p>CSDN是一个中国专业IT社区CSDN (Chinese Software Developer Network) 创立于1999年,致力于为中国软件开发者提供知识传播、在线学习、职业发展等全生命周期服务。</p>
    </div>
    <div class="tab-pane fade" id="ios">
        <p>iOS 是一个由苹果公司开发和发布的手机操作系统。最初是于 2007 年首次发布 iPhone、iPod Touch 和 Apple
            TV。iOS 派生自 OS X,它们共享 Darwin 基础。OS X 操作系统是用在苹果电脑上,iOS 是苹果的移动版本。</p>
    </div>
    <div class="tab-pane fade" id="jmeter">
        <p>jMeter 是一款开源的测试软件。它是 100% 纯 Java 应用程序,用于负载和性能测试。</p>
    </div>
    <div class="tab-pane fade" id="ejb">
        <p>Enterprise Java Beans(EJB)是一个创建高度可扩展性和强大企业级应用程序的开发架构,部署在兼容应用程序服务器(比如 JBOSS、Web Logic 等)的 J2EE 上。
        </p>
    </div>
</div>
<script>
    $(function(){
      
      
        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
      
      
            // 获取已激活的标签页的名称
            var activeTab = $(e.target).text();
            // 获取前一个激活的标签页的名称
            var previousTab = $(e.relatedTarget).text();
            $(".active-tab span").html(activeTab);
            $(".previous-tab span").html(previousTab);
        });
    });
</script>

The result looks like this:
insert image description here

Guess you like

Origin blog.csdn.net/m0_62617719/article/details/131840557
Tab
Tab
Tab
Tab