The methods and principles in this blog post are very detailed and understandable http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html
The following are the basic points and basic code snippets:
There are three main ways to develop jquery plugins:
- Extend jquery with $.extend ()
- Add a new method to jquery through $.fn
- Created using the part factory method of the jquery UI by $.widget ()
The role and usage of extend ()
extend () can pass in one or more objects , overwriting them sequentially, and finally the first object is added to the jquery itself, looking directly at the code:
$.extend ({ function(name) { console.log (' Hello, ' + (name Name: ' Dude ') + '! ') ); // called // call with parameter
Extend () is used in the second method to pass in parameters
A basic way to write plugins
Js:
$.fn.pig =function(options) {varDefaults = { ' Color ': ' Red ', ' FontSize ': ' 12px ' }; varSettings =$.extend ({}, defaults, options); This. css ({' Color ': Settings.color,' Font-size ': Settings.fontsize}); return This. each (function(){ $( This). Append ("' + $ ( This). attr (' href ')); })}
Html:
<ul> <Li> <ahref= "http://www.baidu.com/">Baidu</a> </Li> <Li> <ahref= "http://www.google.com/">Google</a> </Li> <Li> <ahref= "http://www.360.com/">360</a> </Li></ul><P>I'm a PPP.</P><Scripttype= "Text/javascript">$(function() { $('a'). Pig ({'Color': '#2c9929' });})</Script>
The above code, has been able to let the user into the object or do not pass the object, can write a robust and flexible plug-in.
However, when the plug-in content is complex, the code faces organizational problems and can be done with object-oriented thinking:
Object-oriented plug-in development
JS defines the object after the call, and then wraps the anonymous function from the call to prevent pollution of global variables
(function() { //to define the Beautifier constructor varBeautifier =function(Ele, opt) { This. $element =Ele, This. defaults = { ' Color ': ' Red ', ' FontSize ': ' 12px ', ' textdecoration ': ' None ' }, This. Options = $.extend ({}, This. defaults, opt)} //methods for defining BeautifierBeautifier.prototype ={beautify:function() { return This. $element. css ({' Color ': This. Options.color,' FontSize ': This. Options.fontsize,' TextDecoration ': This. options.textdecoration}); } } //using the Beautifier object in a plug- in$.fn.pig =function(options) {//Create a Beautifier entity varBeautifier =NewBeautifier ( This, Options); //call its method returnbeautifier.beautify (); }})();
The HTML does not change
<ul> <Li> <ahref= "http://www.baidu.com/">Baidu</a> </Li> <Li> <ahref= "http://www.google.com/">Google</a> </Li> <Li> <ahref= "http://www.360.com/">360</a> </Li></ul><P>I'm a PPP.</P><Scripttype= "Text/javascript">$(function() { $('a'). Pig ({'Color': '#2c9929' });})</Script>
So far it seems to be close to perfection.
If you think about some potential problems, then Balabalabala, and finally, this looks like this:
;(function($, window, document,undefined) {//to define the Beautifier constructor varBeautifier =function(Ele, opt) { This. $element =Ele, This. defaults = { ' Color ': ' Red ', ' FontSize ': ' 12px ', ' textdecoration ': ' None ' }, This. Options = $.extend ({}, This. defaults, opt)} //methods for defining BeautifierBeautifier.prototype ={beautify:function() { return This. $element. css ({' Color ': This. Options.color,' FontSize ': This. Options.fontsize,' TextDecoration ': This. options.textdecoration}); } } //using the Beautifier object in a plug- in$.fn.myplugin =function(options) {//Create a Beautifier entity varBeautifier =NewBeautifier ( This, Options); //call its method returnbeautifier.beautify (); }}) (JQuery, window, document);
Start writing your own jquery plugin, and the detailed steps to release the jquery plugin to GitHub are also available here: http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html
jquery Plugin Development Code