I have never known how to write plug-ins. Now, I need to know more about it because of my work needs. So I learned some about plug-ins.
In general, plug-ins are a program Writing Method for better extension.
There are two types of query plug-in development:
1, Class-level plug-in development
1.1Add a new global function
To add a global function, we only need to define it as follows:
jQuery.foo = function() { alert(‘This is a test. This is only a test.‘);};
1.2Add multiple global functions
Jquery. foo = function () {alert ('this is a test. this is only a test. ');}; jquery. bar = function (PARAM) {alert ('this function takes a parameter, which is "'+ Param + '". ') ;}; // The call time is the same as that of a function: jquery. foo (); jquery. bar (); or $. foo (); $. bar ('bar'); // You can also. VaR AA = {}; AA. foo = function () {alert ('this is a test. this is only a test. ') ;}; AA. bar = function (PARAM) {alert ('this function takes a parameter, which is "'+ Param + '". ') ;}; AA. foo ();
1.3UseJquery. Extend (object );
jQuery.extend({ foo: function() { alert(‘This is a test. This is only a test.‘); }, bar: function(param) { alert(‘This function takes a parameter, which is "‘ + param +‘".‘); } });
1.4Use namespace
Jquery. myplugin = {FOO: function () {alert ('this is a test. this is only a test. ');}, bar: function (PARAM) {alert ('this function takes a parameter, which is "' + Param + '". ') ;}}; // The namespace function is still a global function. The method used for calling is: $. myplugin. foo (); $. myplugin. bar ('baz ');
2Object-level plug-in development
Object-level plug-in development requires the following two forms:
Form 1:
(function($){ $.fn.extend({ pluginName:function(opt,callback){ // Our plugin implementation code goes here. } }) })(jQuery);
Form 2:
(function($) { $.fn.pluginName = function() { // Our plugin implementation code goes here. }; })(jQuery);
Understand this for the moment, and make up later.
JQ plug-in