ID control tab Switch

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.