jquery plug-in development Sample code _jquery

Source: Internet
Author: User

JQuery Plugin Development:
Class-level development, development of new global functions
Object-level development to develop new methods for jquery objects
Class-level development-defining a global approach

Copy Code code as follows:

Jquery.foo = function () {
Alert (' This is a test. ');
};

Using namespaces, you can avoid the conflicts of functions within namespaces.
Copy Code code as follows:

jquery.apollo={
Fun1:function () {
Console.log (' fun1 ');
},
Fun2:function () {
Console.log (' fun2 ');
}
}


second, object-level development-Define the jquery object approach

Copy Code code as follows:

(function ($) {
$.fn.pluginname = function () {

};
}) (JQuery);
The plug-in is invoked by this way:
$ (' #myDiv '). Pluginname ();

Accept the options parameter to control the behavior of the plug-in
Copy Code code as follows:

(function ($) {
$.fn.fun2=function (option) {
var defaultoption={
param1: ' param1 ',
Param2: ' param2 '
}
$.extend (defaultoption,option);
Console.log (defaultoption);
}
}) (JQuery);
$ (function () {
by calling this
$ ("Body"). Fun2 ({
param1: ' New Param1 '
});
});

Keep Private Functions Private
Copy Code code as follows:

(function ($) {
Plugin definition
$.fn.hilight = function (options) {
Debug (this);
// ...
};
Private Function for debugging
The "Debug" method cannot be entered from an external closure, so it is private for our implementation.
function Debug ($obj) {
if (window.console && window.console.log)
Window.console.log (' Hilight selection count: ' + $obj. Size ());
};
//  ...
}) (JQuery);

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.