JQuery日記 5.31 JQuery對象的產生

來源:互聯網
上載者:User

標籤:snippet   row   迭代   .exe   this   att   shortcut   設定   jquer   

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 stringsif ( 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 checkmatch = [ 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是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 objectthis.length = 1;this[0] = elem;}// 設定內容物件為documentthis.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進行選擇再findreturn 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 presentselector( jQuery );}// 假設selctor是函數或對象時。且有selector元素時if ( selector.selector !== undefined ) {this.selector = selector.selector;this.context = selector.context;}// 將selector放入當前JQuery對象的數組裡return jQuery.makeArray( selector, this );};


JQuery日記 5.31 JQuery對象的產生

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.