You can use the ID method to control the tab switch. The result is very good and the code is very simple.
Last time I encountered a problem. I improved it today and wrote it in the form of plug-ins.
The Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> ID control tab switch jQuery </title>
<Script type = "text/javascript" src = "jquery-1.7.min.js"> </script>
<Script type = "text/javascript">
(Function ($ ){
$. Fn. tab = function (options ){
Var defaultSettings = {
ClassName :"",
Event :""
}
Var s = $. extend (defaultSettings, options );
Return this. each (function (){
Var length = $ (this). find ("a"). length-1;
Var className = $ ("." + s. className );
If (className. length> 1 ){
ClassName. hide ();
ClassName. eq (length). show ();
} Else {
ClassName. eq (0). show ();
}
$ (This). find ("a"). eq (length). addClass ("on ");
$ (This). find ("a"). bind (s. event, function (){
Var node = $ (this). attr ("href"). slice (1 );
$ ("." + S. className). hide ();
$ ("#" + Node). show ();
$ (This). parent (). find ("a"). removeAttr ("class ");
$ (This). addClass ("on ");
Return false;
});
})
}
}) (JQuery)
$ (Function (){
$ ("P"). tab ({
ClassName: "context ",
Event: "click"
});
})
</Script>
<Style type = "text/css">
. On {
Color: # f00;
}
</Style>
</Head>
<Body>
<P> <a href = "# red"> red background </a> <a href = "# green"> green background </a> <a href = "# black "> black background </a> </p>
<Div id = "red" class = "context"> red background </div>
<Div id = "green" class = "context"> green background </div>
<Div id = "black" class = "context"> black background </div>
<Div class = "context"> black background </div>
</Body>
</Html>
P.S: it is best to use a div to wrap the content for Tab switching. Otherwise, problems may occur.