jQuery=function( selector, context ){// The jQuery object is actually just the init constructor 'enhanced'returnnewjQuery.fn.init( selector, context, rootjQuery );}
init:function( selector, context, rootjQuery ){var match, elem;// HANDLE: $(""), $(null), $(undefined), $(false)if(!selector ){returnthis;}// Handle HTML stringsif(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
match =[null, selector,null];}else{
match = rquickExpr.exec( selector );}// Match html or make sure no context is specified for #idif( match &&(match[1]||!context)){// HANDLE: $(html) -> $(array)if( match[1]){
context = context instanceofjQuery? context[0]: context;// scripts is true for back-compat
jQuery.merge(this, jQuery.parseHTML(
match[1],
context && context.nodeType ? context.ownerDocument || context : document,true));// HANDLE: $(html, props)if( rsingleTag.test( match[1])&& jQuery.isPlainObject( context )){for( match in context ){// Properties of context are called as methods if possibleif( jQuery.isFunction(this[ match ])){this[ match ]( context[ match ]);// ...and otherwise set as attributes}else{this.attr( match, context[ match ]);}}}returnthis;// 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 #6963if( elem && elem.parentNode ){// Inject the element directly into the jQuery objectthis.length =1;this[0]= elem;}this.context = document;this.selector = selector;returnthis;}// HANDLE: $(expr, $(...))}elseif(!context || context.jquery ){return( context || rootjQuery ).find( selector );// HANDLE: $(expr, context)// (which is just equivalent to: $(context).find(expr)}else{returnthis.constructor( context ).find( selector );}// HANDLE: $(DOMElement)}elseif( selector.nodeType ){this.context =this[0]= selector;this.length =1;returnthis;// HANDLE: $(function)// Shortcut for document ready}elseif( jQuery.isFunction( selector )){return rootjQuery.ready( selector );}if( selector.selector !== undefined ){this.selector = selector.selector;this.context = selector.context;}return jQuery.makeArray( selector,this);}
// 不写下标就是整体,写了下标就是某一项get:function( num ){return num ==null?// Return a 'clean' arraythis.toArray():// Return just the object( num <0?this[this.length + num ]:this[ num ]);}
pushStack() 用于将一个DOM元素集合加入到jQuery栈
pushStack:function( elems ){// constructor: jQuery/*
jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
return new jQuery.fn.init( selector, context, rootjQuery );
}
*/// this.constructor() 是空的jQuery对象var ret = jQuery.merge(this.constructor(), elems );// 挂载到ret上面,用于回溯(end()方法要使用)
ret.prevObject =this;// 作用域还是之前的作用域
ret.context =this.context;// Return the newly-formed element setreturn ret;},
// 最后上面的span会变红$('div').pushStack($('span')).css('background','red');// 如果想设置div变成黄色$('div').pushStack($('span')).css('background','red').end().css('background','yellow');// ——————————————————————————————————————————————————var test = document.getElementById('demo');$('div').pushStack(test).css('background-color','red').end().css('background-color','yellow');// × test没有length属性$('div').pushStack([test]).css('background-color','red').end().css('background-color','yellow');
first:function(){returnthis.eq(0);},
last:function(){returnthis.eq(-1);},
eq:function( i ){var len =this.length,
j =+i +( i <0? len :0);returnthis.pushStack( j >=0&& j < len ?[this[j]]:[]);},