jQuery源码研究分析学习笔记-jQuery.fn.init()(五)
来源:互联网 发布:巨型加农炮升级数据 编辑:程序博客网 时间:2024/06/06 02:54
$()=jQuery(),二者即是返回的构造函数jQuery.fn.init()实例对象
jQuery = function( selector, context ) { return new jQuery.fn.init( selector, context, rootjQuery );}
jQuery.fn = jQuery.prototype = { constructor: jQuery, init: function( selector, context, rootjQuery ) { //selector:只有undefined、DOM元素、字符串、函数、jQuery对象、普通js对象是有效的,其他类型的值接受没有意义 //context:可选值,传入DOM元素、jQuery对象、普通js对象 //roootjQuery:包含了document对象的jQuery对象,用于document.getElementById()查找失败、selector是选择器表达式且未定context、selctor是函数的情况 var match, elem, ret, doc; // 如果可以转换成false,例如 $(""), $(null), or $(undefined) if ( !selector ) { return this; } // 如果selector有nodeType属性,则此是DOM元素 if ( selector.nodeType ) { //手动设置第一个元素和属性context指向该DOM元素 this.context = this[0] = selector; this.length = 1; //设置length值为1 return this; //返回包含了该dom元素引用的jq对象 } // 如果selector是字符串"body" if ( selector === "body" && !context && document.body ) { //手动设置属性context指向document对象 this.context = document; this[0] = document.body; //设置第一个元素指向body元素 this.selector = selector; this.length = 1; return this; } // 参数selector是其他字符串 if ( typeof selector === "string" ) { // 检测selector是html代码还是#id if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) { // 如果参数selector以"<"开头、以">"结尾,且长度大于等于3,则此字符串是html片段 match = [ null, selector, null ]; } else { //正则quickExpr检测参数selector是否是稍微复杂一些html代码或者#id,匹配结果存放在数组match中 match = quickExpr.exec( selector ); } //参数selector是单独标签, if ( match && (match[1] || !context) ) { // 参数selector是HTML代码 if ( match[1] ) { //先修正context、doc context = context instanceof jQuery ? context[0] : context; doc = ( context ? context.ownerDocument || context : document ); // rsingleTag检测HTML代码是否是单独标签,匹配结果放在数组ret中 // just do a createElement and skip the rest //rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/ ret = rsingleTag.exec( selector ); if ( ret ) { //参数是context是普通对象,则调用jQuery方法.attr()并传入参数context,同时把参数contect中的属性、事件设置到新创建的DOM元素上 if ( jQuery.isPlainObject( context ) ) { selector = [ document.createElement( ret[1] ) ]; jQuery.fn.attr.call( selector, context, true ); } else { selector = [ doc.createElement( ret[1] ) ]; //参数selector是单独标签,调用document.createElement()创建标签对应的DOM元素 } } else { ret = jQuery.buildFragment( [ match[1] ], [ doc ] ); selector = ( ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment ).childNodes; //创建过程 } //将新创建的DOM元素数组合并到当前jQuery对象中并返回 return jQuery.merge( this, selector ); // 参数selector是#id } else { elem = document.getElementById( match[2] ); // 检查parentNode属性,因为Blackberry4.6会返回已经不存在文档中的DOM节点 // nodes that are no longer in the document #6963 if ( elem && elem.parentNode ) { // Handle the case where IE and Opera return items // 所找到元素属性id值与传入的值不相等,则调用Sizzle查找并返回一个含有选中元素的新jQuery对象 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; } // 参数selector是选择器表达式 } 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 ); } // 参数selector是函数 // 绑定ready事件 } else if ( jQuery.isFunction( selector ) ) { return rootjQuery.ready( selector ); } //若参数selector含有属性selector,则认为其是jQuery对象 if ( selector.selector !== undefined ) { this.selector = selector.selector; this.context = selector.context; } //返回当前jQuery对象 return jQuery.makeArray( selector, this ); }};
0 0
- jQuery源码研究分析学习笔记-jQuery.fn.init()(五)
- jQuery源码研究分析学习笔记-jQuery.extend()、jQuery.fn.extend()(八)
- jQuery源码分析-----jQuery.fn.init.prototype=jQuery.fn=jQuery.prototype
- jQuery源码研究分析学习笔记-jQuery.buildFragment()(六)
- jQuery源码学习笔记:jQuery.fn.init(selector,context,rootjQuery)代码详解
- jQuery源码研究分析学习笔记-jQuery.deferred()(12)
- jQuery源码分析 (init)
- jQuery源码研究学习笔记(二)
- jQuery源码研究分析学习笔记-jQuery原型属性和方法(九)
- jQuery源码阅读(五)---init函数
- jQuery.fn.init
- jQuery.fn.init
- jQuery源码中的“new jQuery.fn.init()”什么意思?
- jQuery-toggle()与toggle(fn,fn)的用法 学习笔记五
- jQuery源码研究分析学习笔记-静态方法和属性(10)
- jQuery源码研究分析学习笔记-回调函数(11)
- jQuery.extend()方法和jQuery.fn.extend()方法源码分析
- jQuery学习笔记(五)
- update set命令用来修改表中的数据
- ionic之动态改变icon大小
- C# 跨线程控制lable控件坐标点
- HDU 4707 Pet(BFS,统计树节点深度)
- BT盒子种子搜索神器v4.2.1.0去广告官方绿色版
- jQuery源码研究分析学习笔记-jQuery.fn.init()(五)
- [USACO Training] Section 1.2
- Android官方版本号一览表
- (1)SQL server 创建数据库
- IO流-对象序列化操作流
- 内存的划分
- SQL中的字母的大小写转换
- POJ-3041 Asteroids(最大匹配)
- 网络舆情监控系统爬虫子系统开发心得与不足