Compile the jquery plugin (using the collapsed Panel as an example)

Source: Internet
Author: User

Compile the jquery plugin (using the collapsed Panel as an example)
??

The recommended method for creating a jQuery plug-in is to first create a function that allows the use of the dollar sign ($. By default, dollar signs may conflict with other JavaScript frameworks. If you wrap the plug-in a function, there will be no conflict. We recommend that you use the following code to create the jQuery Plugin:

(Function ($) {$. fn. accordion = function () {// Add plug-in code};}) (jQuery );

 

In the above Code, the jQuery keyword is applied to the wrapper function, which allows you to use the dollar sign in the plug-in, just as when using the fn attribute. After the wrapper function is ready, you can use the dollar sign to replace the jQuery keyword in any part of the plug-in without interfering with other third-party plug-ins.

 

Plug-in code

 

(function($) {  $.fn.accordion = function(options) {    return this.each(function() {      var dts = $(this).children('dt');      dts.click(onClick);      dts.each(reset);    });     function onClick() {      $(this).siblings('dt').each(hide);      if($(this).next().is(:hidden)){      $(this).next().slideDown('fast');        return false;      }      if($(this).next().is(:visible)){         $(this).next().slideUp('fast');        return false;      }          }     function hide() {      $(this).next().slideUp('fast');    }     function reset() {      $(this).next().hide();    }  }})(jQuery);

 

Page code

 <Script src = http://libs.baidu.com/jquery/1.9.0/jquery.js> </script> <script src = aa. js> </script> // plug-in js <script> $ (function () {// call your custom plug-in $ ('dl # my-accordion '). accordion (); // call ins}); </script> 
Section 1
Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.
Section 2
Vestibulum a velit eu ante scelerisque vulputate.
Section 3
Nam mi. Proin viverra leo ut odio. Curabitur malesuada.
Section 4
Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc.

The effect is as follows:

 

 

This is what we want.

 

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.