代码很简单直接上源码
function winnow( elements, qualifier, not ) { //如果qualifier是函数 if ( jQuery.isFunction( qualifier ) ) { //not为true时,返回qualifier返回false的elements //not为false时,返回qualifier返回true的elements return jQuery.grep( elements, function( elem, i ) { /* jshint -W018 */ return !!qualifier.call( elem, i, elem ) !== not; }); } //如果qualifier是DOM元素 if ( qualifier.nodeType ) { //not为true时,返回elements中和qualifier不是同一元素的elements //not为false时,返回elements中和qualifier是同一元素的elements return jQuery.grep( elements, function( elem ) { return ( elem === qualifier ) !== not; }); } //如果qualifier是字符串(将qualifier当成selector) if ( typeof qualifier === "string" ) { // 如果selector不是多级selector(多级selector:div #id)或多selector(selector, selector) // 直接调用jQuery.filter( qualifier, elements, not )返回结果 // risSimple = /^.[^:#\[\.,]*$/ if ( risSimple.test( qualifier ) ) { return jQuery.filter( qualifier, elements, not ); } // 如果selector是多级selector(多级selector:div #id)或多selector(selector, selector) // 解释: 因为多级selector或selector无法直接加上:not组成伪类选择器 // 所以先过滤,再遍历根据not参数过滤 qualifier = jQuery.filter( qualifier, elements ); } return jQuery.grep( elements, function( elem ) { return ( indexOf.call( qualifier, elem ) >= 0 ) !== not; }); } /** * filter方法是对自身进行筛选 * find方法是对子集进行查找或匹配 */ jQuery.filter = function( expr, elems, not ) { var elem = elems[ 0 ]; //如果有not参数,修正selector为not伪类表达式 if ( not ) { expr = ":not(" + expr + ")"; } return elems.length === 1 && elem.nodeType === 1 ? //只有一个元素看是否和表达式匹配 jQuery.find.matchesSelector( elem, expr ) ? [ elem ] : [] : jQuery.find.matches( expr, jQuery.grep( elems, function( elem ) { return elem.nodeType === 1; })); }; jQuery.fn.extend({ find: function( selector ) { var i, len = this.length, ret = [], self = this; // example: // var e = document.getElementById('a'); // var $li = $('div').find(e); if ( typeof selector !== "string" ) { return this.pushStack( jQuery( selector ).filter(function() { for ( i = 0; i < len; i++ ) { // this指向jQuery( selector )迭代的当前元素 // 如果调用.find(selector)的JQuery对象中的的某个元素包含selector,返回true if ( jQuery.contains( self[ i ], this ) ) { return true; } } }) ); } //$('ul').find('li'); for ( i = 0; i < len; i++ ) { jQuery.find( selector, self[ i ], ret ); } // Needed because $( selector, context ) becomes $( context ).find( selector ) // 删除重复元素 ret = this.pushStack( len > 1 ? jQuery.unique( ret ) : ret ); // 拼接调用者和selector,example,$(div).find('ul'),ret.selector为div ul ret.selector = this.selector ? this.selector + " " + selector : selector; return ret; }, // 保留匹配selector的元素 filter: function( selector ) { return this.pushStack( winnow(this, selector || [], false) ); }, // 删除匹配selector的元素 not: function( selector ) { return this.pushStack( winnow(this, selector || [], true) ); }, // 如果调用的jquery元素有一个符合selector返回true is: function( selector ) { return !!winnow( this, // If this is a positional/relative selector, check membership in the returned set // so $("p:first").is("p:last") won't return true for a doc with two "p". typeof selector === "string" && rneedsContext.test( selector ) ? jQuery( selector ) : selector || [], false ).length; }
JQuery日期_5.20 JQuery对Sizzle选择器的扩展,布布扣,bubuko.com
JQuery日期_5.20 JQuery对Sizzle选择器的扩展
原文:http://blog.csdn.net/songzheng_741/article/details/26387453