10 minutes to learn to write jquery plugins

Source: Internet
Author: User

Recently many netizens say jquery plugin is what ah? How do you write that? I can't write? A lot of questions do not know how to answer them at the moment, the personal think is the netizen to complicate the problem. In fact, some of the commonly used, practical, universal functions encapsulated and, in short, is to put these code in a method, you can achieve the effect of repeated use, so that you do not need to use this function every time to write again.
Now jquery is added to the concept of plug-ins, as long as it is in accordance with its specific format as usual writing function to write it, not though it is too complex. Believe it or not, I believe it anyway. Next with a simple code to explain, if the netizens have read the plugin will not write, I can only no language

The first step: Define the plug-in. Code
  1. $ (function () {
  2. $.fn. Plug-in name = function (options) {
  3. var defaults = {
  4. Event: "click",//Trigger response event
  5. Msg: "Holle word!" //display content
  6. };
  7. var options = $.extend (defaults,options);
  8. var $this = $ (this); Of course the response event object
  9. Function Code Section
  10. Binding events
  11. $this. Live (options. Event,function (e) {
  12. alert (options.msg);
  13. });
  14. }
  15. });


Step Two: Call the plugin. Code
    1. $ (function () {
    2. Binding element Events
    3. $ ("#test"). Plug-in name ({
    4. Event: "click",//Trigger response event
    5. Msg: "The plugin was so simple!" //display content
    6. });
    7. });


After reading, I believe that netizens should understand it! The jquery plugin was so simple

10 minutes to learn to write jquery plugins

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.