DOM note (6): How to Implement JQuery extension ?, Domjquery

Source: Internet
Author: User

DOM note (6): How to Implement JQuery extension ?, Domjquery

1. Global Function Extension

A global function adds an independent function to the namespace of JQuery. You can use $. fucnName (param) or jQuery. funcName (param) to call the API.

1. Add a global function sayHello on JQuery directly.

JQuery. sayHello = function (name) {alert (name + "hello ");};


Call method:

JQuery. sayHello ("Zhang zihan"); // or use $. sayHello ("Zhang zihan ");


2. jQuery. extend (): it extends an object by using one or more objects and returns the extended objects.

2.1 jQuery. extend (dsc, src1, src2 ...) : Extend src1 and src2 to the dsc object and return the extended dsc object (merged object)

2.2 jQuery. extend (object): Extension of the jQuery namespace itself. The result is to add a function to the jQuery namespace.

// Add two functions to the jQuery namespace. JQuery. extend ({min: function (a, B) {return a <B? A: B ;}, max: function (a, B) {return a> B? A: B ;}});


Call Method

jQuery.min(2,3); // => 2jQuery.max(4,5); // => 5


Ii. Extension of object Methods

The extension of object methods allows all methods called by jQuery objects.

1. jQuery. fn

// Add the sayHello method jQuery. fn. sayHello = function (name) {alert (name + "") on the jQuery object ");}





2. jQuery. fn. extend

Extends the jQuery element set to provide new methods (usually used to create plug-ins ).

// Add two plug-in methods. JQuery. fn. extend ({check: function () {return this. each (function () {this. checked = true ;}) ;}, uncheck: function () {return this. each (function () {this. checked = false ;});}});





In the next note, we will use a simple example to explain how to develop plug-ins in this way.


Next article: mathematical principles of search and web page ranking

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: 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.