Switch Based on jquery tab (prevent page refreshing)

Source: Internet
Author: User

I found a lot of jquery effects on the Internet, so I wrote one myself. Prevents tab switching during page refreshing,
Html code:
Copy codeThe 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>
<Title> </title>
<Link href = "../Style/admin.css" rel = "stylesheet" type = "text/css"/>
<Script type = "text/javascript" src = "System/js/jquery-1.7.min.js"> </script>
<Script type = "text/javascript" src = "tabs. js"> </script>
<Style type = "text/css">
. Tabs {list-style: none; width: 400px; height: 23px ;}
. Tabs a {display: block; float: left; padding: 2px 3px; background: # eff7ff; text-decoration: none; margin-right: 2px ;}
. Tabs a. current {background: # a1c6de; color: #000 ;}
. Tab {display: block; width: 400px; height: 300px; border: 1px solid # ccc ;}
</Style>
</Head>
<Body>
<Div class = "tabs">
<A href = "# xiangmu"> Project Information </a> <a href = "# kehu"> ask questions about the position of the customer </a> <a href = "# wenda"> </a>
</Div>
<Div id = "xiangmu" class = "tab"> Project Information </div>
<Div id = "kehu" class = "tab"> customer </div>
<Div id = "wenda" class = "tab"> post question </div>
</Body>
</Html>

Js Code:
Copy codeThe Code is as follows:
$ (Function (){
$ ("Div. tab"). hide (); // hide all
$ ("Div. tabs a: first"). addClass ("current"); // select the first element
$ ("Div. tab: first"). show (); // display the first content
$ ("Div. tabs a"). click (function (){
$ ("Div. tabs a"). removeClass ("current"); // remove all tabs from the current style
$ (". Tab"). hide (); // hide all
$ (This). addClass ("current ");
Var activeTab = $ (this). attr ("href"); // obtain the div
$ (ActiveTab). show ();
});
// Obtain
Var url = window. location. href;
Var reg =/#. + /;
If (reg. test (url) {// contains #. The default value is only one #, and multiple # are not considered.
// Hide all
$ ("Div. tabs a"). removeClass ("current"); // remove all tabs from the current style
$ (". Tab"). hide (); // hide all
Var href = url. split ('#') [1];
$ ("Div. tabs [href = #" + href + "]"). addClass ("current ");
$ ("#" + Href). show ();
}
});

The code is very simple and the idea is clear, but it is very practical,
For example, in the previous example, if you want to refresh the page and locate the second tab, you only need to re-specify xxx. aspx # kehu.
Appendix

Related Article

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.