JQuery对象的生成
1 selector为不论什么可转换false的空值
  返回空JQuery对象
2 selector为字符串
  2.1 selector为html字符串或有id属性的标签
      2.2.1 selector为html字符时
            转换html字符为DOM元素并放入当前JQuery的数组
            当context參数为js对象时,迭代器属性
            (1)当前属性相应此JQuery对象的某个函数时,调用此JQuery对象的此函数,參数为当前属性的值.
            (2)其它设置HTML属性
      2.2.2 selector有id时
            直接getElementById
  2.2 selector为选择器表达式
      修正context在context上调用find方法
3 selector为DOM元素
  包装此DOM为JQuery对象
4 selector为函数
  此函数作为ready时间监听器
5 selector为函数及不满足前四个分支的情况
  将此selecotr放入当前JQuery对象数组中

	init = jQuery.fn.init = function( selector, context ) {
var match, elem; // HANDLE: $(""), $(null), $(undefined), $(false)
// 返回空JQuery对象
if ( !selector ) {
return this;
} // Handle HTML strings
if ( typeof selector === "string" ) {
// 假设selector以'<'开头以'>'结尾而且长度大于3觉得其是HTML字符串,不进行rquickExpr匹配
if ( selector[0] === "<" && selector[ 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是jquery对象,取第一个DOM元素
context = context instanceof jQuery ? context[0] : context;
// scripts is true for back-compat
// Intentionally let the error be thrown if parseHTML is not present
// 将HTML转换后的DOM元素合并当到当前jquery对象
jQuery.merge( this, jQuery.parseHTML(
match[1],
context && context.nodeType ? context.ownerDocument || context : document,
true
) ); // HANDLE: $(html, props)
// 处理第一个參数为HTML字符串第二个參数为JS对象
if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
// for-each context中元素
for ( match in context ) {
// Properties of context are called as methods if possible
// 假设当前JQuery对象match属性是函数
if ( jQuery.isFunction( this[ match ] ) ) {
// 运行match函数
this[ match ]( context[ match ] ); // ...and otherwise set as attributes
// 否则,设置HTML属性
} else {
this.attr( match, context[ match ] );
}
}
} return this; // 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
// Blackberry 4.6缓存过度,不在document中的node仍然查找的到
if ( elem && elem.parentNode ) {
// Inject the element directly into the jQuery object
this.length = 1;
this[0] = elem;
}
// 设置上下文对象为document
this.context = document;
this.selector = selector;
return this;
} // HANDLE: $(expr, $(...))
// 处理没有context參数或context參数是JQuery对象
} else if ( !context || context.jquery ) {
// 假设没有context參数则在document范围内调用find方法查找
// 假设有context參数则在本context范围内查找
return ( context || rootjQuery ).find( selector ); // HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
// 处理selector为expr,第二个參数也为context的selector的情况
} else {
// 对context进行选择再find
return this.constructor( context ).find( selector );
} // HANDLE: $(DOMElement)
// 将DOM元素包裹为JQuery对象
} else if ( selector.nodeType ) {
this.context = this[0] = selector;
this.length = 1;
return this; // HANDLE: $(function)
// Shortcut for document ready
// 假设selector是function
// 将function绑定为ready监听,或马上运行(rootjQuery.ready === "undefined")
} else if ( jQuery.isFunction( selector ) ) {
return typeof rootjQuery.ready !== "undefined" ?
rootjQuery.ready( selector ) :
// Execute immediately if ready is not present
selector( jQuery );
}
// 假设selctor是函数或对象时。且有selector元素时
if ( selector.selector !== undefined ) {
this.selector = selector.selector;
this.context = selector.context;
}
// 将selector放入当前JQuery对象的数组里
return jQuery.makeArray( selector, this );
};

JQuery日记 5.31 JQuery对象的生成的更多相关文章

  1. 从零开始学习jQuery (五) 事件与事件对象

    本系列文章导航 从零开始学习jQuery (五) 事件与事件对象 一.摘要 事件是脚本编程的灵魂. 所以本章内容也是jQuery学习的重点. 本文将对jQuery中的事件处理以及事件对象进行详细的讲解 ...

  2. jquery数组内多维对象

    jquery数组内多维对象 var postData=[],obj,list; obj = !!obj ? obj : $('#dist_meici_checkinfo_form'); obj.fin ...

  3. jquery ajax传递多个对象或数组到后台

    1.js对象创建:因为需要把对象json序列化后,才能传递到后台,后台根据json字符串进行反序列化. 2.Jquery   $.ajax方法的配置 针对$.ajax方法的配置参数需要进行修改: 1) ...

  4. JQuery需要手动回收xmlHttpRequest对象

    今天在园子里面看到kuibono的文章说JQuery不会自动回收xmlHttpRequest对象,并且在每次Ajax请求之后都会创建一个新的xmlHttpRequest对象,感到惊讶,索性写了一个程序 ...

  5. jQuery源码笔记——延迟对象

    提供一种方法来执行一个或多个对象的回调函数, Deferred对象通常表示异步事件. 它是回调对象的拓展运用,在jQuery当中非常依赖回调对象. 一个简单的,只解决成功状态下的缓存实例 functi ...

  6. jquery系列教程7-自定义jquery插件全解:对象函数、全局函数、选择器

    点击打开: jquery系列教程1-选择器全解 jquery系列教程2-style样式操作全解 jquery系列教程3-DOM操作全解 jquery系列教程4-事件操作全解 jquery系列教程5-动 ...

  7. jQuery初识、函数、对象

    初识jQuery 官方地址:http://jquery.com/ what:一个优秀的JS函数库(封装了BOM.DOM(主要)) why: HTML元素选取(选择器) HTML元素操作 CSS操作 H ...

  8. jQuery对象合并、jQuery添加静态方法、jQuery添加DOM实例方法

    实例效果: 代码演示: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  9. 通示jQuery实例方法,未DOM对象添加多个方法

    <script type="text/javascript"> /* * 通示jQuery实例方法,未DOM对象添加多个方法 * 用按钮做多个事件的调用 */ (fun ...

随机推荐

  1. Dumb Bones(uva 10529)

    题意:给定n,表示要放n个骨牌,每次放下骨牌,有可能向左倒的概率为pl,向右倒的概率为pr,如果倒下,会将那一侧的骨牌全部推倒,可以选择位置先后放骨牌,问说一种放骨牌次数最少的期望是多少. /* 设d ...

  2. 【NOIP2016】换教室(DP,期望)

    题意: 对于刚上大学的牛牛来说, 他面临的第一个问题是如何根据实际情况中情合适的课程. 在可以选择的课程中,有2n节课程安排在n个时间段上.在第 i ( 1≤ i≤n)个时同段上, 两节内容相同的课程 ...

  3. 【NOIP2016练习】T1 string (计数)

    题意: 思路: ; ..]of int64; n,k,i:longint; ans,x,y:int64; s,t:ansistring; function c(x,y:longint):int64; ...

  4. 手动创建DataTable并绑定gridview

    原文发布时间为:2008-08-04 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  5. 模仿世纪佳缘网站PC端的首页效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Android 4.4 不休眠+不锁屏+默认中文+去除导航栏

    1.不休眠 frameworks/base/packages/SettingsProvider/res/values/defaults.xml 里面60000改成-1,就是不进入休眠. 这个文件还保存 ...

  7. spring+jpa+HiKariCP+P6spy SSH HiKariCP P6spy

    =============p6spy准备https://www.cnblogs.com/qgc88===================== 1.简单介绍p6spy,p6spy是一个开源项目,通常使用 ...

  8. js 去掉以逗号分割的字符串中头尾和中间多余的逗号

    let permission = ",,,106,105,108,,,109,110,107,,101,,," let permission = "106,105,108 ...

  9. SpringBoot系列之(一)helloworld!

    要说什么最流行,现阶段肯定是Springboot和Springcloud,在Spring官方网站上第一个就是springboot,可见对springboot的重视程度.主要原因springboot集成 ...

  10. SSL/TLS协议

    今天闲着给自己的网站申请了一个免费证书,顺便复习下SSL/TLS协议    (https 就是在http+ssl协议) SSL介绍: 安全套接字(Secure Socket Layer,SSL)协议是 ...