After using this long time jQuery, I have never written much about the jQuery plug-in. Today, we have implemented two plug-ins to consolidate the basic knowledge. This article only describes how to compile your own plug-ins. As for the functions to be implemented, they may vary from person to person...
JQuery plug-ins are mainly divided into three types:
1. Plug-ins that encapsulate object Methods
2. Plug-ins that encapsulate global functions
3. Extension selector plug-in
Here we only write the first two types, which are more common ..
Most plug-ins are written in this form:
The Code is as follows:
(Function ($ ){
/* Place the code here */
}) (JQuery );
The advantage is that the function can still use $ as the alias of jQuery, without affecting the usage of $
JQuery provides two extensions for writing plug-ins.
$. Fn. extend ({}); used to expand the first type
$. Extend ({}); used to expand the second type
The following is the implementation result and code
The Code is as follows: