Custom plug-in development tutorial in jquery

Source: Internet
Author: User
Tags extend min

An extension of global functions

A global function is to add a separate function to the namespace central of jquery. When used, it can be invoked either through $.fucnname (param) or jquery.funcname (param).

1. Add directly to jquery: Add a global function to jquery SayHello

The code is as follows
Jquery.sayhello=function (name)
{
Alert (name+ "Hello");
};

Call Mode:

The code is as follows
Jquery.sayhello ("Zhang Zihan");
or use $.sayhello ("Zhang Zihan");

2, Jquery.extend (): Its role is to use one or more objects to extend an object, to return the extended object.

2.1 Jquery.extend (Dsc,src1,src2 ...): extends the SRC1,SRC2 to the DSC object and returns the Extended DSC object (the merged object)

2.2 Jquery.extend (object): Expands the jquery namespace itself, resulting in the addition of functions to the jquery namespace.

The code is as follows

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 mode

The code is as follows
Jquery.min (2,3); => 2
Jquery.max (4,5); => 5

Ii. extension of Object methods

The extension of the object method allows all the methods called by the jquery object.

1, Jquery.fn

The code is as follows

Add the SayHello method to the JQuery object
Jquery.fn.sayhello=function (name)
{
Alert (name+ "Hello");
}

Call

$ ("div"). SayHello ("Dwqs");

2, JQuery.fn.extend

Expand the set of JQuery elements to provide new methods (usually used to make plug-ins).

  code is as follows &nbs P;

//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 ();

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.