jquery Plugin Development

Source: Internet
Author: User

There are two types of jquery plugin development:

One is the development of a class-level plug-in that adds a new global function to jquery, which is equivalent to adding a method to the jquery class itself. The global function of jquery is a function that belongs to the jquery namespace, and the other is the object-level plug-in development that adds a method to the jquery object. The following is a detailed description of the development of the two functions.

Class-level plug-in development

Custom Global functions

Jquery.foo = function () {     alert (' This is a test. This was only a test. ');  Jquery.bar = function (param) {     alert (' This function takes a parameter, which is "' + param + '". ');  };   

Using Jquery.extend (object)

Jquery.extend ({        foo:function () {          alert (' This is a test. This was only a test. ')        ,        bar:function (param) {          alert (' This function takes a parameter, which is "' + para M + ' ". ');        }     

  

Object-level plug-in development

Plug-in development at the object level requires two forms:

Form 1:

(function ($) {       $.fn.extend ({         pluginname:function ($) {opt,callback) {               //Our plugin implementation code goes here.           }       })     

  

Form 2:

(function ($) {         $.fn.pluginname = function () {          //Plugin Implementation code goes here       };     

  

The above defines a jquery function, the parameter is $, and after the function definition is complete, the jquery argument is passed in. Call execution immediately. The advantage of this is that when we write a jquery plugin, we can also use this alias, without causing a conflict with prototype.

Call samples such as Next

$ (' #myDiv '). Hilight ();

  

Accept the options parameter to control plug-in behavior

Plugin definition    $.fn.hilight = function (options) {      var defaults = {        foreground: ' Red ',        background: ' Yellow '      };      Extend Our default options with those provided.      var opts = $.extend (defaults, options);      Our plugin implementation code goes here.    };    Our plugin can be called this way:  $ (' #myDiv '). Hilight ({      foreground: ' Blue ')    

  

Resources

jquery Plugin Development Full analysis

jquery Plugin Development

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.