Home  >  Article  >  Web Front-end  >  Parameter passing method for jQuery object initialization_jquery

Parameter passing method for jQuery object initialization_jquery

WBOY
WBOYOriginal
2016-05-16 16:12:441226browse

The parameter passing methods for jQuery object initialization include:

1.$(DOMElement)
2.$('

...

'), $('#id'), $('.class') passes in a string. This is the most common form. This pass Parameters are often passed in as the second parameter context to specify the context, where the context parameter can be $(...), DOMElement
3.$(function() {}); <===> $(document).ready(function() { });
4.$({selector : '.class', context : context}) <===> $('.class', context)

Copy code The code is as follows:

jQuery.fn = jQuery.prototype = {
Constructor: jQuery,
init: function(selector, context, rootjQuery) {
var match, elem, ret, doc;
​​​​ // Process the parameters $(""), $(null), $(undefined), $(false) and directly return this
           if ( !selector ) {
              return this;
}
                                                            // When the passed parameter selector is a DOM node, set the context to selector
If (selector.nodeType) {
This.context = this[0] = selector;
This.length = 1;
              return this;
}
               // Handle HTML strings
// When the incoming 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.exec( 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
        // 当selector为function时相当于$(document).ready(selector);
        } else if ( jQuery.isFunction( selector ) ) {
            return rootjQuery.ready( selector );
        }
        // 当selector参数为{selector:'#id', context:document}之类时,重置属性selector和context
        if ( selector.selector !== undefined ) {
            this.selector = selector.selector;
            this.context = selector.context;
        }
        return jQuery.makeArray( selector, this );
    }
};

以上就是本文的全部内容了,希望大家能够喜欢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn