jquery Plugin Development Code

Source: Internet
Author: User

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:
    1. Extend jquery with $.extend ()
    2. Add a new method to jquery through $.fn
    3. 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

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.