Writing jquery Plugins

Source: Internet
Author: User

jquery's plugins are divided into three main categories:
1. Encapsulation method Plug-in
Encapsulation method plug-in In essence, is an object-level plug-in, such plug-ins first get objects through the jquery selector, and add methods to the object, then, the method is packaged, closed into a plug-in, this type of plug-in writing simple, very easy to call, It also makes it easy to use the powerful selectors in jquery, making it the first choice for developing plug-ins.
2. Encapsulating function Plug-in
The closed function plug-in is a class-level plug-in, the biggest feature of this type of plug-in is that you can add a static method directly to jquery, and you can place the function in the jquery namespace, such as $.ajax (), $.trim () global function, are embedded in the jquery kernel in the form of internal plugins.
3. Selector plugin: Expand some of your favorite selectors.

1;(function($){2$.fn.name =function(options) {//various properties, Parameters3varoptions = $.extend (defaults, options);//Defining Parameters4 This. each (function(){5//plug-in implementation code6            return  This;7        });8     };9 Ten$.fn.highlight.defaults = { One         //Default Property Parameters A     }; - }) (jQuery); -  the //called -$.fn.name.defaults.x = ' '; -$ (' #id '). Name ();

(1), ";(function ($) {})" (JQuery); An anonymous function is used, which means "$" in the method body means "jquery", for better compatibility, so add a semicolon in front
, where the $ sign is the formal parameter of the anonymous function
(2), $.extend (defaults, options); indicates that if the parameters in the options always have values, then the values in the options will replace the values in the defaults.

JQuery extend () and jQuery.fn.extend () are called in different ways:

Jquery.extend (), generally called by the incoming global function, is mainly used to expand a global function, such as $.init (), $.ajax ();

JQuery.fn.extend (), typically called by specific instance objects, can be used to extend a selector, such as $.fn.each ();

The main functions of jQuery extend () and jQuery.fn.extend () are different:

Jquery.extend(object); To extend the jquery class itself, add a new method for itself.

JQuery.fn.extend(object); Add a method to a jquery object

Writing 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.