首页  >  文章  >  web前端  >  jQuery对象初始化的传参方式_jquery

jQuery对象初始化的传参方式_jquery

WBOY
WBOY原创
2016-05-16 16:12:441237浏览

jQuery对象初始化的传参方式包括:

1.$(DOMElement)
2.$('

...

'), $('#id'), $('.class') 传入字符串, 这是最常见的形式, 这种传参数经常也传入第二个参数context指定上下文,其中context参数可以为$(...), DOMElement
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;
        // 处理$(""), $(null), $(undefined), $(false)这几种参数,直接返回this
        if ( !selector ) {
            return this;
        }
        // 当传参selector为DOM结点时,将context置为selector
        if ( selector.nodeType ) {
            this.context = this[0] = selector;
            this.length = 1;
            return this;
        }
        // Handle HTML strings
        // 当传入的selector参数为字符串时,
        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 ) {
                        // 处理 IE 和 Opera 返回项目的情况
                        // 按名称而不是 ID
                        if ( elem.id !== match[2] ) {
                            return rootjQuery.find( 选择器 );
                        }
                        // 否则,我们将元素直接注入到 jQuery 对象中
                        this.length = 1;
                        this[0] = elem;
                    }
                    this.context = 文档;
                    this.selector = 选择器;
                    返回此;
                }
            // 句柄:$(expr, $(...))
            } else if ( !context || context.jquery ) {
                return ( context || rootjQuery ).find( 选择器 );
            // 句柄:$(expr, context)
            // (相当于:$(context).find(expr)
            } 其他 {
                return this.constructor( context ).find( 选择器 );
            }
        // 句柄:$(function)
        // 文档准备好的快捷方式
        // 当selector为function时相当于$(document).ready(selector);
        } else if ( jQuery.isFunction( 选择器 ) ) {
            return rootjQuery.ready( 选择器 );
        }
        //当selector参数为{selector:'#id', context:document}之类时,重置属性selector和context
        if ( 选择器.selector !== 未定义 ) {
            this.selector = 选择器.selector;
            this.context = 选择器.context;
        }
        return jQuery.makeArray( 选择器, this );
    }
};

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

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn