【jQuery源码】jQuery对象初始化
看了一下午还是有很多地方没弄明白,jQuery的一些工具方法的原理也不完全清楚,这篇文章会随着我深入阅读jQuery源码的同时不断更新。
// Initialize a jQuery object
// 初始化Jquery对象 // A central reference to the root jQuery(document)
var rootjQuery, //
Use the correct document accordingly with window argument (sandbox)
document = window.document, // A simple way to check for HTML strings
// Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
// Strict HTML recognition (#11290: must start with <)
// 检测html字符串的正则表达式
rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/, init = jQuery.fn.init = function( selector, context ) {
var match, elem; // HANDLE: $(""), $(null), $(undefined), $(false)
if ( !selector ) {
return this;
} // Handle HTML strings
// 下面的match中,match[0]存放原字符串,match[1]存html标签,match[2]存#q中的“q”
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
//selector是HTML标签,类似<div>123</div>
match = [ null, selector, null ]; } else {
//不是html标签
match = rquickExpr.exec( selector );
} // Match html or make sure no context is specified for #id
// 匹配的html或确保没有上下文指定为# id
if ( match && (match[1] || !context) ) { // HANDLE: $(html) -> $(array)
// 1.处理html标签
if ( match[1] ) {
context = context instanceof jQuery ? context[0] : context; // scripts is true for back-compat
// Intentionally let the error be thrown if parseHTML is not present
//jQuery.merge( first, second ) 合并两个数组内容到第一个数组
//jQuery.parseHTML()函数用于将HTML字符串解析为对应的DOM节点数组
//对于HTML文档来说,documentElement是<html>标签对应的Element对象,ownerDocument是document对象
jQuery.merge( this, jQuery.parseHTML(
match[1],
context && context.nodeType ? context.ownerDocument || context : document,
true
) ); // HANDLE: $(html, props)
// rsingleTag匹配匹配独立标签,比如'<html></html>'或者'<div></div>'
if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
for ( match in context ) {
// Properties of context are called as methods if possible
if ( jQuery.isFunction( this[ match ] ) ) {
this[ match ]( context[ match ] ); // ...and otherwise set as attributes
} else {
this.attr( match, context[ match ] );
}
}
} return this; // HANDLE: $(#id)
//2.处理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, $(...))
//3.处理表达式
} else if ( !context || context.jquery ) {
return ( context || rootjQuery ).find( selector ); // HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
//4.处理有上下文的表达式
} else {
return this.constructor( context ).find( selector );
} // HANDLE: $(DOMElement)
//5.处理DOM元素
} else if ( selector.nodeType ) {
this.context = this[0] = selector;
this.length = 1;
return this; // HANDLE: $(function)
// Shortcut for document ready
//6.处理函数
} else if ( jQuery.isFunction( selector ) ) {
return typeof rootjQuery.ready !== "undefined" ?
rootjQuery.ready( selector ) :
// Execute immediately if ready is not present
selector( jQuery );
} if ( selector.selector !== undefined ) {
this.selector = selector.selector;
this.context = selector.context;
} return jQuery.makeArray( selector, this );
};
【jQuery源码】jQuery对象初始化的更多相关文章
- JQuery源码之“对象的结构解析”
吃完午饭,觉得有点发困,想起了以后我们的产品可能要做到各种浏览器的兼容于是乎不得不清醒起来!我们的web项目多数是依赖于Jquery的.据了解,在Jquery的2.0版本以后对IE的低端版本浏览器不再 ...
- jquery源码'jQuery.fn.init.prototype'
一般我们在创建构造函数即使用的时候会这样写,使用的时候会使用new 关键字,先实例化,然后使用. function test(name, age) { this.name = name; this.a ...
- jQuery源码解析对象实例化与jQuery原型及整体构建模型分析(一)
//源码剖析都基于jQuery-2.0.3版本,主要考虑到兼容IE 一.关于jQuery对象实例化的逻辑: 整个jQuery程序被包裹在一个匿名自执行行数内: (function(window,und ...
- jQuery源码-jQuery.fn.attr与jQuery.fn.prop
jQuery.fn.attr.jQuery.fn.prop的区别 假设页面有下面这么个标签,$('#ddd').attr('nick').$('#ddd').prop('nick')分别会取得什么值? ...
- 读jQuery源码 jQuery.data
var rbrace = /(?:\{[\s\S]*\}|\[[\s\S]*\])$/, rmultiDash = /([A-Z])/g; function internalData( elem, n ...
- JQuery源码解析-- 对象的创建
使用 $("a") 返回的对象就不再是一个简单的DOM对象了,而是一个复杂的JQuery对象. 那么JQuery是怎么创建对象的. 为了便于分析,我将JQuery中复杂的代码简化了 ...
- JQuery源码-------JQuery中数值型变量的判断isNumeric
判断一个数值型变量的方法,在jquery中非常简单,只有一行代码. isNumeric: function( obj ) { // parseFloat NaNs numeric-cast false ...
- jQuery源码的一个坑
纯吐槽 大半夜也真是够了,想学着jQ造个小轮子巩固下js,结果一开始就卡住了. 虽然之前也看过源码,但是主要是研究方法实现什么的,对于框架主函数和入口结构不怎么熟悉,于是想着一步一步调试看看. $(' ...
- jquery 源码解析
静态与实力方法共享设计 遍历方法 $(".a").each() //作为实例方法存在 $.each() //作为静态方法存在 Jquery源码 jQuery.prototype = ...
- jQuery源码解析资源便签
最近开始解读jQuery源码,下面的链接都是搜过来的,当然妙味课堂 有相关的一系列视频,长达100多期,就像一只蜗牛慢慢爬, 至少品读三个框架,以后可以打打怪,自己造造轮子. 完全理解jQuery源代 ...
随机推荐
- Python是什么
Python是一种编程语言,它的名字来源于一个喜剧.也许最初设计Python这种语言的人并没有想到今天Python会在工业和科研上获得如此广泛的使用.著名的自由软件作者Eric Raymond在他的文 ...
- (网络流 模板 Dinic) Drainage Ditches --POJ --1273
链接: http://poj.org/problem?id=1273 代码: //Dinic #include<stdio.h> #include<string.h> #inc ...
- bolg迁移
博客已迁移至:http://www.s0nnet.com 欢迎大家继续关注!!! 2015-7-4
- oracle 游标实现多重循环
declare -- Local variables here i integer; cursor c_province is select ds.swjg_dm from dm_swjg ds wh ...
- Android-AndroidManifest.xml默认启动的Activity(探索篇01)
AndroidManifest.xml-->默认启动 MusicBrowserActivity <activity android:name=".MusicBrowserAct ...
- Backup--还原选项之STANDBY
很多DBA对还原时制定RECOVERY 与 NORECOVERY选项都很熟悉,但是对于STANDBY就有点茫然了,今天一起来学习下吧. --============================== ...
- supervisord 启动失败 Error: Another program is already listening on a port that one of our HTTP serve...
Linux系统中 Supervisor 配置守护进程: 启动Supervisor 服务语句: supervisord -c /etc/supervisor/supervisord.conf 这个过程可 ...
- PHP7 - 如何禁用Xdebug
操作系统:Cent OS 7 什么是Xdebug?看它的官方网站介绍: https://xdebug.org/index.php 为什么要禁用呢?这是因为Composer,这两种工具有冲突,在使用Co ...
- “全栈2019”Java多线程第十九章:死锁详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- Binary Search Tree-530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...