Create the jquery plug-in tabs step by step (Ajax only requests results once, 78 rows are completed)

Source: Internet
Author: User

Tabs is one of the most widely used Web pages. There are also many jquery plug-ins and non-jquery plug-ins. Some friends asked me how to use jquery. UI. Tabs ajax to request the server only once.

I thought it was actually very simple. I knew it from the official API, but I checked the official UI with firebug before replying to these friends. tabs found that AJAX cache is declared. When a tabs is clicked, there will still be server requests

This should be the server cache, rather than the actual requirement of Ajax only once, and no longer requests the server

Next, I looked for other tabs plug-ins, which basically did not meet the requirements. They were either too large or too simple. If they were too large, I 'd better use the UI. tabs, documentation andCodeStandards are reliable

Therefore, it is necessary to make a simple tabs plug-in.

Before the design, sort out the ideas, implement the main functions such as tabs, automatic rotation, and Ajax, and then arrange the Dom. Here we use the traditional

<Div id = "tabs">

<Ul>

<Li> <a href = "# tabs1"> tabs1 </a> </LI>

<Li> <a href = "# tabs2" rel = "ajax.htm"> tabs2 </a> </LI>

</Ul>

<Div id = "tabs1"> Hello world! </Div>

<Div id = "tabs2"> </div>

</Div>

A Li corresponds to a div. When Ajax is used, add a rel attribute of a, write the content to the corresponding Div, and then remove the rel attribute. In this way, the server is requested only once, next, all the content that DIV has written.

I didn't use cookies here. I can use jquery. Cookie plug-in combination, so that even if the user closes the web page and then opens it again, the server does not need to be requested.

I. First, write a closure of the jquery plug-in. A friend in the garden wrote the Javascript closure concept these two days. It's good. If you are interested, go and check it out.

 
(Function ($) {// code here}) (jquery );

2. Plug-in name. The name here is atabs. In this way, $ (...). atabs () can be used for binding. My English name is Allen, So I named it with the header ~

 
$. FN. atabs = function (options) {// API // main function}

3. Write the desired function as an API for external Modification

$. FN. atabs. defaults = {firston: 0, classname: 'selected', eventname: 'all', // click, mouserover, all loadname: 'loading... ', // Ajax wait string fadein: 'normal', autofade: false, autofadetime: 3}; var opts = $. extend ({}, $. FN. atabs. defaults, options); // Replace the default value of the external input here, $. for more information about how extend works, see http://api.jquery.com/jquery.extend /.

4. Compile the main function and describe the comments in the code.

Return this. each (function () {// The VaR target =$ (this); var DIV = target for each bound Dom plug-in. children (). not ("ul, span"); // all tabs display body Div var tabs = target. find ('ul: eq (0) li '); // all tabs head indexes function tabs () {if ($ (this ). hasclass (OPTs. classname) {return false;} tabsshow (DIV, $ (this); Return false;} function tabsshow (DIV, Li, index) {Div. stop (True, true ). hide (); // use if (typeof (INDEX) = "Number "){ If (Li. find (""). ATTR ("rel") Ajax (DIV, Li); $ (div [Index]). stop (True, true ). fadein (OPTs. fadein); tabs. stop (True, true ). removeclass (OPTs. classname); $ (tabs [Index]). stop (True, true ). addclass (OPTs. classname);} // The else {var tabbody = div. filter (Li. find (""). ATTR ("href"); If (Li. find (""). ATTR ("rel") Ajax (DIV, Li); tabbody. stop (True, true ). fadein (OPTs. fadein); tabs. stop (True, true ). removeclass (Opts. classname); Li. stop (True, true ). addclass (OPTs. classname) ;}} function Ajax (DIV, Li) {// here is the key Ajax. By operating Rel, you can request the server var href = Li only once. find (""). ATTR ("href"); var rel = Li. find (""). ATTR ("rel"); // Ajax request URL var I = div. filter (href); // current Div if (rel) {// If the Ajax request URL is not empty, only Ajax once I .html (OPTs. loadname); $. ajax ({URL: Rel, cache: false, success: function (HTML) {I .html (HTML) ;}, error: function () {I .html ('loading error, please try again! ') ;}}); Li. find (""). removeattr ("rel"); // Ajax only once} If (OPTs. autofade) {var Index = opts. firston + 1; setinterval (function () {If (index> = div. length) {Index = 0;} tabsshow (DIV, $ (this), index ++) ;}, opts. autofadetime * 1000);} tabs. BIND (OPTs. eventname = 'all '? 'Click mouseover': opts. eventname, tabs) // bind event. Filter (': First'). Trigger (OPTs. eventname = 'all '? 'Click': opts. eventname); // automatically triggered event });

Finally, the tabs plug-in is born after the above integration. The following is the full source code:

/** Author: Blackstone's rock */(function ($) {$. FN. atabs = function (options) {$. FN. atabs. defaults = {firston: 0, classname: 'selected', eventname: 'all', // click, mouserover, all loadname: 'loading... ', // Ajax wait string fadein: 'normal', autofade: false, autofadetime: 3}; var opts = $. extend ({}, $. FN. atabs. defaults, options); return this. each (function () {var target = $ (this); var DIV = target. children (). not ("ul, span ");/ /All tabs display bodies Div var tabs = target. find ('ul: eq (0) li '); // all tabs head indexes function tabs () {if ($ (this ). hasclass (OPTs. classname) {return false;} tabsshow (DIV, $ (this); Return false;} function tabsshow (DIV, Li, index) {Div. stop (True, true ). hide (); // use if (typeof (INDEX) = "Number") {If (Li. find (""). ATTR ("rel") Ajax (DIV, Li); $ (div [Index]). stop (True, true ). fadein (OPTs. fadein); tabs. stop (true, True ). removeclass (OPTs. classname); $ (tabs [Index]). stop (True, true ). addclass (OPTs. classname);} // The else {var tabbody = div. filter (Li. find (""). ATTR ("href"); If (Li. find (""). ATTR ("rel") Ajax (DIV, Li); tabbody. stop (True, true ). fadein (OPTs. fadein); tabs. stop (True, true ). removeclass (OPTs. classname); Li. stop (True, true ). addclass (OPTs. classname) ;}} function Ajax (DIV, Li) {var href = Li. find (" A "). ATTR ("href"); var rel = Li. find (""). ATTR ("rel"); // Ajax request URL var I = div. filter (href); // current Div if (rel) {// If the Ajax request URL is not empty, only Ajax once I .html (OPTs. loadname); $. ajax ({URL: Rel, cache: false, success: function (HTML) {I .html (HTML) ;}, error: function () {I .html ('loading error, please try again! ') ;}}); Li. find (""). removeattr ("rel"); // Ajax only once} If (OPTs. autofade) {var Index = opts. firston + 1; setinterval (function () {If (index> = div. length) {Index = 0;} tabsshow (DIV, $ (this), index ++) ;}, opts. autofadetime * 1000);} tabs. BIND (OPTs. eventname = 'all '? 'Click mouseover': opts. eventname, tabs) // bind event. Filter (': First'). Trigger (OPTs. eventname = 'all '? 'Click': opts. eventname); // automatically triggered event}) ;};} (jquery );

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.