Jquery Extension Method

Source: Internet
Author: User
There are two types of jquery plug-in development: one is class-level plug-in development, that is, adding a new global function to jquery is equivalent to adding a method to the jquery class itself. Jquery's global functions belong to the jquery namespace, and the other is object-level plug-in development. There are two types of jQuery object plug-in development: one is class-level plug-in development, that is, adding a new global function to jquery is equivalent to adding a method to the jquery class itself. Jquery's global functions belong to the jquery namespace, and the other is object-level plug-in development, that is, adding methods to jQuery objects. It can also be understood as two other methods: one is the extension method of jQuery itself, and the other is the extension method of the object selected by jQuery. 1. jQuery. extend (Object); // jQuery extension method 2. jQuery. fn. extent (Object); // The extension methods of the objects selected by jQuery are described in detail below for the development of the two functions. 1. The most direct understanding of plug-in development at the class level is to add class methods to the jQuery class, which can be understood as adding static methods. A typical example is the $. AJAX () function, which is defined in the namespace of jQuery. The following extensions can be used for class-level plug-in development: 1.1 Add a new global function [javascript] $. foo = function () {alert ("foo");} // call method $. foo (); 1.2 use jQuery. extend (object); [javascript] $. extend ({foo: function () {alert ("foo") ;}, bar: function () {alert ("bar") ;}}) // call method $. foo (); $. bar (); 1.3 Use namespace [javascript] $. myPlugin = {foo: function () {alert ("foo") ;}, bar: function () {alert ("bar") ;}// call method $. myPlugin. foo (); $. myPlugin. bar (); 2. Object-level plug-in development [javascript] $. fn. foo = function () {alert ("hello jquery") ;}$ ('# myf '). foo () [javascript] (function ($) {$. fn. extend ({pluginName: function (opt, callback) {alert ("foo") ;}}) (jQuery); $ ("# myF "). pluginName (); [javascript] (function (jq) {jq. fn. foo = function (obj) {alert (obj) ;}}) (jQuery) $ ("# myF "). foo ("hello jquery ");
Related Article

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.