Jquery.extend (), jQuery.fn.extend () extension method detailed

Source: Internet
Author: User

jQuery自定义了jQuery.extend()和jQuery.fn.extend()方法.其中jQuery.extend()方法能够创建全局函数或者选择器,而jQuery.fn.extend()方法能够创建jQuery对象方法.
例如:

Jquery.extend ({    showname:function (name) {        alert (name)    }}); Jquery.showname ("Deep Blue");

jQuery.extend()除了可以创建插件外,还可以用来扩展jQuery对象.
例如:

var a = {    name: "Blue",    pass:123}var b = {    name: "Red",    pass:456,    age:1}var c = Jquery.exten D ({},a,b);

c拥有a,b对象的属性,由于,b对象在a对象之后,其name属性优先在c对象里.

jQuery.extend()方法为插件传递系列选项,包括默认值.

function fn (options) {    var options = jquery.extend ({    //default parameter Options list        name1:value1,        name2:value2,        Name3:value3    },options);   Use the parameters of the function to overwrite or merge into the default parameter options list    //function body}FN ({name1:value3, name2:value2, name3:value1});//Use the new value FN ({name4:value3, name 5:value2});//Add a new option to the default FN ();    Keep Default option values

当在调用该方法时,传递新的参数值,就会覆盖掉默认的参数选项值,否则,使用默认参数值.


使用JQuery.fn对象创建JQuery对象方法

可以通过jQuery.fn对象来添加属性和方法,实际上jQuery.fn对象就是挂接在jQuery.prototype上的,jQuery把它简写了.

What's the FN thing? If you look at the jquery code, it's not hard to find.


Jquery.fn = Jquery.prototype = {

Init:function (Selector, context) {//....

//......

};

The original Jquery.fn = Jquery.prototype. It's certainly not strange to prototype.


例如:
JQuery.fn.test = function () {    alert ("This is the JQuery object Method!");} JQuery ("div"). Click (function () {    $ (this). Test ();   Call the test () method} on the current jquery object);


我们可以调用jQuery.fn.extend()方法来创建jQuery对象方法.

JQuery.fn.extend ({    test:function () {        return This.each (function () {            alert (this.nodename)});}    ); JQuery ("Body *"). Click (function () {    $ (this). Test ();     Invoke jquery object method});

Word: Jquery.extend is a custom extension to the jquery class, JQuery.fn.extend is a custom extension to the jquery object.

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.