Reputation: 140042
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.
Upvotes: 17
Views: 85256
Reputation: 15233
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>
Upvotes: 3
Reputation: 944171
TabTastic is a good guide — it is accessible, and (when JavaScript is not available) fails very gracefully.
Upvotes: 3
Reputation: 100856
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).
Upvotes: 0
Reputation: 78890
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>
Upvotes: 31
Reputation: 31508
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 :)
Upvotes: 0
Reputation: 272367
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
Upvotes: 1