jQuery对象初始化的传参方式包括:
1.$(DOMElement)
2.$('<h1>...</h1>'), $('#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 ) {
                        // 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 );
    }
};

jQuery对象初始化的多种传参数形式的更多相关文章

  1. {{jQuery源码分析}}jQuery对象初始化的多种传参数形式

    jQuery对象初始化的传参方式包括:1.$(DOMElement)2.$('<h1>...</h1>'), $('#id'), $('.class') 传入字符串, 这是最常 ...

  2. jQuery对象初始化的传参方式

    jQuery对象初始化的传参方式包括: 1.$(DOMElement) 2.$(' ... '), $('#id'), $('.class') 传入字符串, 这是最常见的形式, 这种传参数经常也传入第 ...

  3. 【jQuery源码】jQuery对象初始化

    看了一下午还是有很多地方没弄明白,jQuery的一些工具方法的原理也不完全清楚,这篇文章会随着我深入阅读jQuery源码的同时不断更新. // Initialize a jQuery object / ...

  4. Objective-C对象初始化 、 实例方法和参数 、 类方法 、 工厂方法 、 单例模式

    1 重构Point2类 1.1 问题 本案例使用初始化方法重构Point2类,类中有横坐标x.纵坐标y两个属性,并且有一个能显示位置show方法.在主程序中创建两个Point2类的对象,设置其横纵坐标 ...

  5. jQuery1.11源码分析(9)-----初始化jQuery对象的函数和关联节点获取函数

    这篇也没什么好说的,初始化jQuery对象的函数要处理多种情况,已经被寒冬吐槽烂了.关联节点获取函数主要基于两个工具函数dir和sibling,前者基于指定的方向遍历,后者则遍历兄弟节点(真的不能合并 ...

  6. C#语法糖之第二篇: 参数默认值和命名参数 对象初始化器与集合初始化器

    今天继续写上一篇文章C#4.0语法糖之第二篇,在开始今天的文章之前感谢各位园友的支持,通过昨天写的文章,今天有很多园友们也提出了文章中的一些不足,再次感谢这些关心我的园友,在以后些文章的过程中不断的完 ...

  7. 第七节:语法总结(1)(自动属性、out参数、对象初始化器、var和dynamic等)

    一. 语法糖简介   语法糖也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方 ...

  8. ECharts-初始化方法参数不能传入jquery对象

    ECharts-初始化方法参数不能传入jquery对象

  9. jquery对象里面的context参数

    jquery源码: jQuery = function( selector, context ) { // The jQuery object is actually just the init co ...

随机推荐

  1. 整理低版本ie兼容问题的解决方案

    CSS hack \9    所有的IE10及之前 *     IE7以及IE7以下版本的 _     IE6以及IE6以下版本的      !important  提升样式优先级权重 1.ie6,7 ...

  2. IllegalArgumentException: Unmatched braces in the pattern.

    IllegalArgumentException: Unmatched braces in the pattern. 非法参数异常. 不匹配的 吊带 在 样品 中. === 没有管.  项目一直卡在d ...

  3. BZOJ3009 : 集合

    取一棵生成森林,根据题目限制可得,与一个点相连的多余的边数是$O(\sqrt{m})$级别的. 对于树边,每个点维护3棵权值线段树,依次保存它的儿子里各个集合的边. 再开3*3个分块数组,记录多余边以 ...

  4. Centos7 安装 ActiveMQ 5.15.1

    环境 [root@node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@node1 ~]# uname -r -.el ...

  5. C# 如何实现邮件发送

    调用发送 try { P2BEmail email = new P2BEmail(); email.fromEmail = txtfromEmail.Text; // QQ邮箱 email.fromP ...

  6. MySQL错误:TIMESTAMP with implicit DEFAULT value is deprecated

    用于存放数据库的文件夹不为空,清空了再来一次!

  7. 一些收集的MikroTik RouterOS破解版虚拟机VMware

    会不定期更新,也许后续自己来做,现在是收集.持续关注这个分享链接即可. 链接:https://pan.baidu.com/s/1j7ciesPm1yAgCJ26dLJ8sg  密码:i64w

  8. Software UART, Timer, PWM, External Interrupt

    How can you add extra hardware UARTs to a 32bit TMS470 ARM7-based microcontroller at zero cost? Solu ...

  9. Synchronize Ultimate

    支持多种服务器和主流云网盘进行同步 http://www.icecoldapps.com/ Unlock Code : xda201506 Unlock Code : icecoldapps20150 ...

  10. 未能加载文件或程序集“System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项

    在发布网站时遇到如题错误. 错误消息详细信息:未能加载文件或程序集“System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=3 ...