JQuery日記 6.3 JQuery遍曆模組

來源:互聯網
上載者:User

標籤:des   c   class   blog   code   java   

jQuery.extend({// 返回elem延DOM樹某個方向訪問的所有節點,直到遇到until條件dir: function( elem, dir, until ) {var matched = [],truncate = until !== undefined;while ( (elem = elem[ dir ]) && elem.nodeType !== 9 ) {if ( elem.nodeType === 1 ) {if ( truncate && jQuery( elem ).is( until ) ) {break;}matched.push( elem );}}return matched;},//返回n的兄弟節點(把n,elem設定成相同元素時,不返回本身)sibling: function( n, elem ) {var matched = [];for ( ; n; n = n.nextSibling ) {if ( n.nodeType === 1 && n !== elem ) {matched.push( n );}}return matched;}});jQuery.fn.extend({has: function( target ) {// 以當前jquery對象為上下文尋找targetvar targets = jQuery( target, this ),l = targets.length;// 過濾掉當前JQuery不包含target後代的元素return this.filter(function() {var i = 0;for ( ; i < l; i++ ) {if ( jQuery.contains( this, targets[i] ) ) {return true;}}});},// 返回包含符合selector元素的最近的JQuery對象// 返回的可能是其包含自己或其父類的JQuery對象,其長度為0或1closest: function( selectors, context ) {var cur,i = 0,l = this.length,matched = [],pos = rneedsContext.test( selectors ) || typeof selectors !== "string" ?jQuery( selectors, context || this.context ) :0;for ( ; i < l; i++ ) {// 從本身開始,然後沿父類繼續for ( cur = this[i]; cur && cur !== context; cur = cur.parentNode ) {// Always skip document fragments// 如果pos是以$(selectors,context)尋找出的JQuery對象if ( cur.nodeType < 11 && (pos ?// pos中是否可尋找到cur元素pos.index(cur) > -1 :// 否則查看cur是否滿足selectors選擇運算式// Don't pass non-elements to Sizzlecur.nodeType === 1 &&jQuery.find.matchesSelector(cur, selectors)) ) {matched.push( cur );break;}}}return this.pushStack( matched.length > 1 ? jQuery.unique( matched ) : matched );},// Determine the position of an element within// the matched set of elementsindex: function( elem ) {// No argument, return index in parent// 沒有傳入參數返回在同輩元素中的位置if ( !elem ) {return ( this[ 0 ] && this[ 0 ].parentNode ) ? this.first().prevAll().length : -1;}// index in selector// 參數是選取器if ( typeof elem === "string" ) {// 返回本JQuery對象第一個元素在匹配所有此選取器的JQuery對象的位置return indexOf.call( jQuery( elem ), this[ 0 ] );}// Locate the position of the desired element// 返回參數元素在此JQuery對象的位置return indexOf.call( this,// If it receives a jQuery object, the first element is usedelem.jquery ? elem[ 0 ] : elem);},// 在當前JQuery對象中添加額外元素add: function( selector, context ) {return this.pushStack(jQuery.unique(jQuery.merge( this.get(), jQuery( selector, context ) )));},// 將調用鏈上前一個JQuery中的元素添加到當前JQuery對象中// selector參數用於過濾前一個JQuery對象addBack: function( selector ) {return this.add( selector == null ?this.prevObject : this.prevObject.filter(selector));}});// 沿DOM樹某個方向擷取下一個DOM元素function sibling( cur, dir ) {// 一直遇到DOM元素再停下while ( (cur = cur[dir]) && cur.nodeType !== 1 ) {}return cur;}// 綁定快捷遍曆JQuery對象方法jQuery.each({parent: function( elem ) {var parent = elem.parentNode;return parent && parent.nodeType !== 11 ? parent : null;},parents: function( elem ) {return jQuery.dir( elem, "parentNode" );},parentsUntil: function( elem, i, until ) {return jQuery.dir( elem, "parentNode", until );},next: function( elem ) {return sibling( elem, "nextSibling" );},prev: function( elem ) {return sibling( elem, "previousSibling" );},nextAll: function( elem ) {return jQuery.dir( elem, "nextSibling" );},prevAll: function( elem ) {return jQuery.dir( elem, "previousSibling" );},nextUntil: function( elem, i, until ) {return jQuery.dir( elem, "nextSibling", until );},prevUntil: function( elem, i, until ) {return jQuery.dir( elem, "previousSibling", until );},siblings: function( elem ) {return jQuery.sibling( ( elem.parentNode || {} ).firstChild, elem );},children: function( elem ) {return jQuery.sibling( elem.firstChild );},// frame裡的所有子項目contents: function( elem ) {return elem.contentDocument || jQuery.merge( [], elem.childNodes );}}, function( name, fn ) {// 在JQuery的原型對象上綁定快捷遍曆函數jQuery.fn[ name ] = function( until, selector ) {// this指調用此方法的JQuery對象// 根據不同遍曆的fn,將this中的元素映射成其遍曆後的元素var matched = jQuery.map( this, fn, until );// 如果方法不是沒有終止條件(Until)// 修正selector為第一個參數if ( name.slice( -5 ) !== "Until" ) {selector = until;}if ( selector && typeof selector === "string" ) {// 過濾matched中不符合selecotr的元素matched = jQuery.filter( selector, matched );}// this中如果有多個元素,那麼遍曆後的matched可能存在重複if ( this.length > 1 ) {// Remove duplicatesif ( !guaranteedUnique[ name ] ) {jQuery.unique( matched );}// Reverse order for parents* and prev-derivatives// parent或pare的遍曆matched應該反轉,使matched順序更符合邏輯if ( rparentsprev.test( name ) ) {matched.reverse();}}// 返回以matched為元素的新的JQuery對象return this.pushStack( matched );};});

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.