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