How do I make a tabbed view in HTML?

蓝咒 提交于 2019-12-04 16:29:32

问题


When clicking on tab A, show content for tab A. Click on tab B, show content for tab B, and so on.

What's the most simple and compatible way of constructing a HTML snippet?

I don't mean to use any libraries here, so none of jQuery or any other libraries.


回答1:


If you want to roll your own tab control, you could do something like this:

<html>
  <head>
    <script type="text/javascript">

      function activateTab(pageId) {
          var tabCtrl = document.getElementById('tabCtrl');
          var pageToActivate = document.getElementById(pageId);
          for (var i = 0; i < tabCtrl.childNodes.length; i++) {
              var node = tabCtrl.childNodes[i];
              if (node.nodeType == 1) { /* Element */
                  node.style.display = (node == pageToActivate) ? 'block' : 'none';
              }
          }
      }

    </script>
  </head>
  <body>
    <ul>
      <li>
        <a href="javascript:activateTab('page1')">Tab 1</a>
      </li>
      <li>
        <a href="javascript:activateTab('page2')">Tab 2</a>
      </li>
      ...
    </ul>
    <div id="tabCtrl">
      <div id="page1" style="display: block;">Page 1</div>
      <div id="page2" style="display: none;">Page 2</div>
      ...
    </div>
  </body>
</html>



回答2:


Here is a list of different types of tabs plus tutorials on how to build them




回答3:


TabTastic is a good guide — it is accessible, and (when JavaScript is not available) fails very gracefully.




回答4:


If you want to implement your own tab view, just do it like this:

<html>
    <head>
        <style>
            .tab {
            display:none;
            }
        </style>

        <script type="text/javascript">
          function initTabView(){
            var x = document.getElementsByClassName('tab-view')
            for(var i=0; i < x.length; i++) {
              x[i].onclick = displayTab;
            }

            var prevViewedTab = null;

            function displayTab(e) {
            var idOfTabToDisplay = this.getAttribute("data-tab")

            if(prevViewedTab) {
              prevViewedTab.style.display = 'none';
            }

            var tabToDisplay = document.getElementById(idOfTabToDisplay);
              tabToDisplay.style.display = 'block';
              prevViewedTab = tabToDisplay;
            }

            var defaultTab = document.getElementsByClassName('default-tab')
              if (defaultTab.length) {
                defaultTab[0].style.display = 'block';
                prevViewedTab = defaultTab[0];
              }
          }
        </script>
    </head>

    <body>
        <ul>
            <li>
                <a data-tab="tab1" class="tab-view">Tab 1</a>
            </li>
            <li>
                <a data-tab="tab2" class="tab-view">Tab 2</a>
            </li>
            <li>
                <a data-tab="tab3" class="tab-view">Tab 3</a>
            </li>
            <li>
                <a data-tab="tab4" class="tab-view">Tab 4</a>
            </li>
        </ul>
        <div id="tabCtrl">
            <div class="tab default-tab" id="tab1">This is Tab 1</div>
            <div class="tab" id="tab2">This is Tab 2</div>
            <div class="tab" id="tab3">This is Tab 3</div>
            <div class="tab" id="tab4">This is Tab 4</div>
        </div>

        <script>
            initTabView();
        </script>
    </body>
</html>

A jsFiddle is available here.




回答5:


Take a look at an example such as this (courtesy of a Google search for 'tabbed view javascript').

You can obviously use this with a little customisation, but it's instructive to take it apart and determine what it's doing. It's basically enabling or disabling <div> using the display style and setting it to block or none




回答6:


If "simple and compatible" are your only two criteria, I'd suggest the jQuery UI Tabs plugin. Cross-browser, and a cinch to implement.




回答7:


Depending on your ambitions, it could simply be a matter of an unordered list and a number of <div>s (tab contents). Then a simple JavaScript could - by means of getElementById() - set the display property for all the <div>s: none for all except the current.

Alternatively, you could have a look at this.

Edit: Not the only one linking to the jQuery site, it seems :)




回答8:


The tabs widget in jQuery UI is easy to use: http://jqueryui.com/demos/tabs/.

The jQuery tabs widget works completely on the browser side - content for all tabs are sent on every request, or you could write JavaScript code that uses Ajax to load the tab contents dynamically.

But it might not be appropriate for your use. Consider if you need to control the tabs server-side (that is, a click on a tab sends a new page request to the server - the server constructs HTML that has the visual appearance of tabs).




回答9:


If you're open to using JavaScript, jQuery's tab is pretty nice.



来源:https://stackoverflow.com/questions/1027663/how-do-i-make-a-tabbed-view-in-html

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