jQuery源代码学习笔记:jQuery.fn.init(selector,context,rootjQuery)代码具体解释
3.1 源代码
init: function( selector, context, rootjQuery ) {
var match, elem, ret, doc;
// Handle $(""), $(null), or $(undefined)
//假设selector为空格。!selector为false
if (!selector) {
//此时this为空jQuery对象
return this;
}
// Handle $(DOMElement)
//nodeType节点类型。利用是否有nodeType属性来推断是否是DOM元素
if ( selector.nodeType ) {
//将第一个元素和属性context指向selector
this.context = this[0] = selector;
this.length = 1;
return this;
}
// The body element only exists once, optimize finding it
//由于body仅仅出现一次。利用!context进行优化
if ( selector === "body" && !context && document.body ) {
//context指向document对象
this.context = document;
this[0] = document.body;
this.selector = selector;
this.length = 1;
return this;
}
// Handle HTML strings
if ( typeof selector === "string" ) {
// Are we dealing with HTML string or an ID?
//以<开头以>结尾,且长度大于等于3。这里假设是HTML片段,跳过queckExpr正则检查
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 = quickExpr.exec( selector );
}
// Verify a match, and that no context was specified for #id
if ( match && (match[1] || !context) ) {
// HANDLE: $(html) -> $(array)
if ( match[1] ) {
context = context instanceof jQuery ? context[0] : context;
doc = ( context ?
context.ownerDocument || context : document );
// If a single string is passed in and it's a single tag
// just do a createElement and skip the rest
ret = rsingleTag.exec( selector );
//假设是单独标签
if (ret) {
//假设context是普通对象
if (jQuery.isPlainObject(context)) {
//之所以放在数组中。是方便后面的jQuery.merge()方法调用
selector = [document.createElement(ret[1])];
//调用attr方法。传入參数context
jQuery.fn.attr.call( selector, context, true );
} else {
selector = [ doc.createElement( ret[1] ) ];
}
//复杂HTML的处理方法
} else {
ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
selector = ( ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment ).childNodes;
}
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
//即使是documen.getElementById这样核心的方法也要考虑到浏览器兼容问题,可能找到的是name而不是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, $(...))
//没有指定上下文,运行rootjQuery.find()。制定了上下文且上下文是jQuery对象,运行context.find()
} else if ( !context || context.jquery ) {
return ( context || rootjQuery ).find( selector );
// HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
//假设指定了上下文。且上下文不是jQuery对象
} else {
//先创建一个包括context的jQuery对象。然后调用find方法
return this.constructor( context ).find( selector );
}
// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
}
//selector是jquery对象
if ( selector.selector !== undefined ) {
this.selector = selector.selector;
this.context = selector.context;
}
//合并到当前jQuery对象
return jQuery.makeArray( selector, this );
},
当中里面调用的其它jQuery函数待后面再具体学习。
1、当中用到了两个正則表達式:
(1)quickExpr = /^(?
:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/;
解释:?:是非捕获组的意思。非捕获组仅仅參与匹配。但不会把匹配到的内容捕获到组里,减少了内存的占用。
[^#<]*匹配除#<以外的随意字符,且出现0次或多次
(<[\w\W]+>):
\w表示匹配字母数字、下划线,\W正好相反,这里匹配的是以字母数字下划线作为第一个字母的一个标签,比方<div>
[^>]*匹配除>之外的全部字符,且出现0次或多次,$结束,表示结束时不能为>字符
#(\w\-]*)$ #id的匹配。id仅仅能为字母、数字、下划线和减号
(2)rsingleTag = /^<(\w+)\s*\/?
>(?:<\/\1>)?$/;
解释:^<(\w+)\s*\/?
>:
^<:以<开头
(\w+):匹配的字母、数字、下划线
\s*:匹配空格符0次或多次,比方能够匹配<div >这种
\/?>:匹配/符号0次或1次。比方匹配<img />这种
(?:<\/\1>):\1表示的是前面的(),这里指的就是(\w+),比方前面是<div>。这里就要是</div>
最后的?$表示的是此元素为截止符,要么截止在</div>,要么没有就是前面的<img >或者是<img />
到此就把全部的单独的标签的情况考虑全然了。
学以致用
<script type="text/javascript">
var quickExpr = /^(? :[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/;
var rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/; var match = quickExpr.exec("abc<div>#id");
if (match) {
alert(RegExp.$1)
} var match1 = rsingleTag.exec("<div></div>");
if (match1) {
alert(RegExp.$1);
}
</script>
jQuery源代码学习笔记:jQuery.fn.init(selector,context,rootjQuery)代码具体解释的更多相关文章
- jQuery源代码学习笔记:构造jQuery对象
2.1源代码结构: (function( window, undefined ) { var jQuery = (function() { // 构建jQuery对象 var jQuery = fun ...
- jQuery源代码学习之八——jQuery属性操作模块
一.jQuery属性模块整体介绍 jQuery的属性操作模块分四个部分:html属性操作,dom属性操作,类样式操作,和值操作. html属性操作(setAttribute/getAttribute) ...
- jQuery源代码学习之六——jQuery数据缓存Data
一.jQuery数据缓存基本原理 jQuery数据缓存就两个全局Data对象,data_user以及data_priv; 这两个对象分别用于缓存用户自定义数据和内部数据: 以data_user为例,所 ...
- jQuery源代码学习之五——jQuery.when
jQuery.when提供了基于一个或多个对象的状态来执行回调函数的功能,通常是基于具有异步事件的异步队列. 如果传入多个异步队列,jQuery.when会返回一个新的主异步队列的只读副本(promi ...
- jQuery源代码学习之四——jQuery.callbacks
自己实现的callbacks模块相较于jquery源代码中的callbacks模块有所简化,表面上看没有考虑firing这个参数,没有对之进行任何处理,即没有考虑在函数执行过程中,再次调用add,re ...
- jQuery源代码学习笔记_工具函数_noop/error/now/trim
jQuery源代码学习笔记_工具函数_noop/error/now/trim jquery提供了一系列的工具函数,用于支持其运行,今天主要分析noop/error/now/trim这4个函数: 1.n ...
- jQuery源代码学习笔记_01
如何获取jQuery源代码 1.可以从GitHub上下载到没有合并和压缩的源代码 2.如果要查看兼容IE6-8的版本,请选择1.x-master分支 3.可以使用git clone也可以使用downl ...
- jQuery源代码学习笔记_bind
一般想到JS的兼容性问题的时候,首先会想到addEventListener与attachEvent这一对冤家,那么我们先来看看它们有什么兼容性问题 addEventListener与attachEve ...
- jQuery源代码学习之九—jQuery事件模块
jQuery事件系统并没有将事件坚挺函数直接绑定在DOM元素上,而是基于事件缓存模块来管理监听函数的. 二.jQuery事件模块的代码结构 //定义了一些正则 // // //jQuery事件对象 j ...
随机推荐
- 【HTML+CSS】浅谈:相对定位与绝对定位
相对定位和绝对定位 ·定位标签:position ·包括属性:relative(相对) absolute(绝对) 1.position:relative; 假设对一个元素进行相对定位.首先它将出如今 ...
- C程序设计语言(K&R)笔记
1.表达式中float类型的操作数不会自动转换为double类型.一般来说,数学函数(如math.h)使用双精度类型的变量.使用float类型主要是为了在使用较大数组时节省存储空间,有时也为了节省机器 ...
- 每日一发linux命令
很多用虚拟机的同学在向/tmp目录下进行解压的时候,会发现之前挂载的此目录空间不足,导致下一步无法进行(我在vmwaretools解压的时候就遇到了这个problem)…… 实际上,/tmp是可以进行 ...
- win7 下面使用任务计划程序执行php脚步
1.操作系统中点击开始->所有程序->附件->系统工具->任务计划程序 2.如下图 3.下一步,如图: . 4.下一步,如图 5.下一步,如下图: 6.这样设置好以后,就可以了 ...
- sql必知必会(第四版) 学习笔记二 视图
本书用到的几个表的建表sql语句如下: --销售产品供应商 CREATE TABLE Vendors ( vend_id varchar(20) not null, vend_name varchar ...
- 16进制字符串转换为byte数组
/// <summary> /// 16进制字符转换为byte数组 /// </summary> /// <param name="hexString" ...
- Android 启动Service服务和发送Broadcast广播的常用方法
一.先说Service服务. 1.利用setAction()方法来指定启动的Service服务 Intent intent = new Intent(); intent.setAction(" ...
- oracle中闪回错误的dml操作原理
原理: Oracle根据还原表空间信息,利用还原表空间中的数据,类似一致性读取方法,可以把表置于一个删除前的时间点(或SCN),从而将数据找回. 删除删除错误的dml操作的oracle的前提准备: 一 ...
- jQuery Validate 插件验证,,返回不同信息(json remote)自定义
问题 申请账号需要确认该账号是存在 jquery.validate.js中的remote Jquery Ajax获取后台返回的Json数据后,添加自定义校验 解题思路:输入的登陆信息远程验证是否该账号 ...
- 安装apk到虚拟的device
adb device 显示你的设备 adb install apk包