Jquery plugin Learning (5)

Source: Internet
Author: User

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 ");

Related Article

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.