JQuery日记 5.31 JQuery对象的生成
2014-05-31 13:18
357 查看
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对象数组中
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 ); };
相关文章推荐
- JS,JQuery循环数组,循环对象生成需要的数据
- jQuery生成json对象
- 黑马程序员_学习日记73_725ASP.NET(禁用Cookie、ASP.NET中的重要对象、JavaScript实现AJAX、两种Json格式的序列化方法、JQuery中实现ajax的五种方法)
- jquery.flot.js 生成的图表无法在IE6及部分IE8上显示的问题之一 'window.G_vmlCanvasManager' 为空或不是对象
- PowerDesigner11中批量根据对象的name生成comment的脚本
- 一段由数据库结构生成相应数据对象的代码(1)(2) (3)--DataHelper
- 编写完成:组件对象字符串生成程序 V2.0 修正版
- 我的用CodeSmith生成的业务对象2
- 如何根据类的名字来生成对象
- 只能用new生成的对象
- 用hibernate-extensions自动生成POJO对象
- 控件对象字符串生成程序 2.1 升级版本 一句话:太方便了
- 用.net生成Google的SiteMaps的XML规范文件。(利用XmlTextWriter对象)
- 用hibernate-extensions自动生成POJO对象
- jsp读取大对象CLOB并生成xml文件示例
- 自动生成数据对象代码和CRUD操作的C#代码生成器
- C++动态对象生成技术
- 动态生成对象
- 提供一个更为完善的XMLHttp对象生成法
- 由父对象实例生成子对象实例