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.
If "simple and compatible" are your only two criteria, I'd suggest the jQuery UI Tabs plugin. Cross-browser, and a cinch to implement.
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 ofgetElementById()
- 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 :)
If you want to roll your own tab control, you could do something like this:
If you're open to using JavaScript, jQuery's tab is pretty nice.
If you want to implement your own tab view, just do it like this:
A jsFiddle is available here.
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).