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

Source: Internet
Author: User

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

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 ");}

Call

$("div").sayHello("dwqs");

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 ;});}});

Call:

$("input[type=checkbox]").check();$("input[type=radio]").uncheck();

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

First: http://www.ido321.com/1330.html

Next article: mathematical principles of search and web page ranking

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.