Multiple parameter passing modes for jQuery object initialization. jquery Initialization

Source: Internet
Author: User

Multiple parameter passing modes for jQuery object initialization. jquery Initialization

Parameters initialized by jQuery objects include:
1. $ (DOMElement)
2. $ ('3. $ (function () {}); <==>$ (document). ready (function (){});
4. $ ({selector: '. class', context: context}) <==>$ ('. class', context)

JQuery. fn = jQuery. prototype = {
Constructor: jQuery,
Init: function (selector, context, rootjQuery ){
Var match, elem, ret, doc;

// Process the $ (""), $ (null), $ (undefined), $ (false) parameters and directly return this
If (! Selector ){
Return this;
}

// Set context to selector when the parameter selector is set to DOM.
If (selector. nodeType ){
This. context = this [0] = selector;
This. length = 1;
Return this;
}

// Handle HTML strings
// When the input selector parameter is a string,
If (typeof selector = "string "){
If (selector. charAt (0) = "<" & selector. charAt (selector. length-1) ==="> "& selector. length> = 3 ){
// Assume that strings that start and end with <> are HTML and skip the regex check
Match = [null, selector, null];

} Else {
Match = rquickExpr.exe c (selector );
}

// Match html or make sure no context is specified for # id
If (match & (match [1] |! Context )){

// HANDLE: $ (html)-> $ (array)
If (match [1]) {
Context = context instanceof jQuery? Context [0]: context;
Doc = (context & context. nodeType? Context. ownerDocument | context: document );

// Scripts is true for back-compat
Selector = jQuery. parseHTML (match [1], doc, true );
If (rsingleTag. test (match [1]) & jQuery. isPlainObject (context )){
This. attr. call (selector, context, true );
}

Return jQuery. merge (this, selector );

// HANDLE: $ (# id)
} Else {
Elem = document. getElementById (match [2]);

// Check parentNode to catch when Blackberry 4.6 returns
// Nodes that are no longer in the document #6963
If (elem & elem. parentNode ){
// Handle the case where IE and Opera return items
// By name instead of ID
If (elem. id! = Match [2]) {
Return rootjQuery. find (selector );
}

// Otherwise, we inject the element directly into the jQuery object
This. length = 1;
This [0] = elem;
}

This. context = document;
This. selector = selector;
Return this;
}

// HANDLE: $ (expr, $ (...))
} Else if (! Context | context. jquery ){
Return (context | rootjQuery). find (selector );

// HANDLE: $ (expr, context)
// (Which is just equivalent to: $ (context). find (expr)
} Else {
Return this. constructor (context). find (selector );
}

// HANDLE: $ (function)
// Shortcut for document ready
// When selector is a function, it is equivalent to $ (document). ready (selector );
} Else if (jQuery. isFunction (selector )){
Return rootjQuery. ready (selector );
}

// When the selector parameter is {selector: '# id', context: document}, reset the attributes selector and context
If (selector. selector! = Undefined ){
This. selector = selector. selector;
This. context = selector. context;
}

Return jQuery. makeArray (selector, this );
}
};

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.