This section encapsulates the plug-in. How is the progress?
Generally, plug-ins released externally should be encapsulated, And the encapsulated plug-ins should comply with the specifications. Only such plug-ins can be popularized and favored by other users.
The first step is to define an independent domain. The Code is as follows.
Copy codeThe Code is as follows:
(Function ($ ){
// Custom plug-in code
}) (JQuery) // encapsulate the plug-in
Determine the plug-in creation type and select the creation method. For example, to create a plug-in that sets the element font color, you should create the jquery object method. Considering that jquery provides the extension method extended by plug-in (), calling this method is more standard.
Copy codeThe Code is as follows:
(Function ($ ){
// Custom plug-in code
$. Extend ($. fn, {// jquery object Extension Method
})
}) (JQuery) // encapsulate the plug-in
Generally, the plug-in accepts parameters to control the actions of the plug-in. For example, the plug-in that sets the color should allow the user to set the font color. At the same time, consider that if the user does not set the color, you should keep the default color for setting.
Copy codeThe Code is as follows:
(Function ($ ){
// Custom plug-in code
$. Extend ($. fn ,{
Color: function (options ){
Var options = $. extend ({bcolor: "white", fcolor: "black"}, options );
//
}
})
}) (JQuery) // encapsulate the plug-in
Finally, complete the plug-in
Copy codeThe Code is as follows:
; (Function ($ ){
$. Extend ($. fn ,{
Color: function (options) {var options = $. extend ({bcolor: "white", fcolor: "black"}, options );
// Function body
Return this. each (function (){
((This).css ("color", options. bcolor );
Certificate (this).css ("background", options. fcolor );
});
} // Color = end
})
}) (JQuery );
Call to see
Copy codeThe Code is as follows:
$ ("H1"). color ({bcolor: "# ccc", fcolor: "# eee "});
$ ('A'). color ("# fff ");