jquery Plug-in Learning (v) _jquery

Source: Internet
Author: User
This section encapsulates Plug-ins, what's the progress?

The general external release of Plug-ins should be packaged, packaged plug-ins should also be in line with the specification, only such a written plug-in to have a promotional value, and other users love.

The first step is to define a separate field, as shown in the following code.
Copy Code code as follows:

(function ($) {
Custom Plug-in Code
}) (JQuery)//Encapsulation Plugin

To determine the type of plug-in to create, choose how to create it, for example, to create a plug-in that sets the element font color, you should create a JQuery object method that is more canonical when you consider that jquery provides a plug-in extension method extend ().
Copy Code code as follows:

(function ($) {
Custom Plug-in Code
$.extend ($.fn,{//jquery Object extension method

})
}) (JQuery)//Encapsulation Plugin

Generic Plug-ins will accept parameters to control the behavior of Plug-ins, for example, for color-setting plug-ins, you should allow the user to set the font color, and should be considered if the user does not set the color, you should leave the default color to set.
Copy Code code as follows:

(function ($) {
Custom Plug-in Code
$.extend ($.fn,{
Color:function (options) {
var options = $.extend ({bcolor: "White", Fcolor: "Black"},options);
//
}
})
}) (JQuery)//Encapsulation Plugin

Finally improve the plugin
Copy Code code 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);
$ (this). CSS ("background", options.fcolor);
});
}//color==end
})
}) (JQuery);

Call to see
Copy Code code as follows:

$ ("H1"). Color ({bcolor: "#ccc", Fcolor: "#eee"});
$ (' a '). Color ("#fff");

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.