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