jQuery構(gòu)造函數(shù)init參數(shù)分析續(xù)
如果selector是其他字符串情況就比較多了比較復雜了
// Handle HTML strings if ( typeof selector === "string" ) {...}
開始分不同的情況處理
// Are we dealing with HTML string or an ID? 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 = quickExpr.exec( selector ); }
If里面先判斷第一個字符是“<”最后一個字符是“>”并且長度大于3就假設此時的selector是html簡單標簽 ,比如$(‘<div>')但是記住僅僅是假設”assume”比如$(‘<sdfadfadf>')這樣的也會走這里。然后把match數(shù)組修改成[null,selector,null],這里的match是在init函數(shù)里面聲明的變量,主要是用來作為區(qū)分是參數(shù)類型的工具稍后在將可能情況列出,下面是源碼中聲明的四個變量
init: function( selector, context, rootjQuery ) { var match, elem, ret, doc;
如果不滿足if的條件就會調(diào)用一個正則去得到match的結(jié)果,quickExpr是jQuery構(gòu)造函數(shù)里面聲明的變量
// A simple way to check for HTML strings or ID strings // Prioritize #id over <tag> to avoid XSS via location.hash (#9521) quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
這個正則主要是為了區(qū)別html字符串和id字符串的,第二個注釋中講到了為了避免基于 location.hash的 XSS 攻擊,于是在 quickExpr 中增加了 #(#9521)的意思是我們可以在jQuery官網(wǎng)找到相關解釋。
首先訪問http://bugs.jquery.com/然后搜索對應的值即可
quickExpr.exec( selector )執(zhí)行的結(jié)果可以是一個數(shù)組,數(shù)組的第一個元素是匹配的元素,剩下的分別是分組匹配的元素,這個正則有兩個分組(<[\w\W]+>)[^>]和([\w\-]*)一個是標簽一個是id值。最終會把結(jié)果交給match。下面就來分析下match的各種情況首先單標簽不用正則式是 [ null, selector, null ]的形式,下面在代碼中證明:
<!doctype html> <html> <head> <title></title> <script src='jquery-1.7.1.js'></script> </head> <body> <div id='div'></div> </body> <script> $('<div>'); </script> </html>
在html里面我們創(chuàng)建一個jQuery對象然后再init方法里面輸出得到的match結(jié)果:
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 = quickExpr.exec( selector ); } console.log(match); // [null, "<div>", null];
下面我們修改一下參數(shù)改為$(‘#div')然后再看一下結(jié)果
["#div", undefined, "div", index: 0, input: "#div"]
還有一種比較特殊的情況$(‘<div>123')然后我們再看一下結(jié)果
["<div>dewrwe", "<div>", undefined, index: 0, input: "<div>dewrwe"]
我們可以看到id總是在第三個元素而標簽值在第二個元素保存著,對于最后一種情況而言跟$(‘<div>')是沒有什么區(qū)別的因為生成dom元素時是不會處理第一個元素的?;谶@個結(jié)果可以接著來分析下一個判斷了。
接下來的會根據(jù)match的結(jié)果分為三種情況
if ( match && (match[1] || !context) ) { ... } else if ( !context || context.jquery ) { ... } else { ... }
第一種情況滿足的條件是match一定要有值,match[1]就是第二個元素就是保存標簽的這個有值或者不存在上下文,但是好像沒有id什么事???其實不是的通過分析match的結(jié)果可以知道第二個元素沒有值肯定就是id選擇器得到的結(jié)果,而id是唯一的,不需要寫上下文(其實寫了上下文也會正常執(zhí)行只不過會使用Sizzle而不是在這里處理了跟body是一樣的)。好了第一個條件進來的情況就是
1.標簽
$(‘<div>') $(‘<div>123') $(‘<div>23213213</div>')...
2.沒有上下文的id $(‘#div')
第一個條件內(nèi)部又進行了細分:
// HANDLE: $(html) -> $(array) if ( match[1] ) { ... // HANDLE: $("#id") }else{ }
很顯然if是處理標簽的else是處理id的,先來看看是怎么處理標簽的吧
context = context instanceof jQuery ? context[0] : context; doc = ( context ? context.ownerDocument || context : document ); // If a single string is passed in and it's a single tag // just do a createElement and skip the rest ret = rsingleTag.exec( selector ); if ( ret ) { if ( jQuery.isPlainObject( context ) ) { selector = [ document.createElement( ret[1] ) ]; jQuery.fn.attr.call( selector, context, true ); } else { selector = [ doc.createElement( ret[1] ) ]; } } else { ret = jQuery.buildFragment( [ match[1] ], [ doc ] ); selector = ( ret.cacheable ? jQuery.clone(ret.fragment) : ret.fragment ).childNodes; } return jQuery.merge( this, selector);
首先修正一下context的值,如果是jQuery對象就把他變成dom元素就是使用下標的方法這個原理之前說過了,然后有處理了doc變量,如果context不存在就把document賦值給doc如果存在且有ownerDocument屬性那就是dom元素了這個值還是document如果不是dom元素比如普通的js對象的話那就把這個對象賦值給doc變量。緊接著對selector又進行了一個正則判斷,這個正則也是在jQuery構(gòu)造函數(shù)里面聲明的目的是判斷單標簽 比如<div>這樣的
// Match a standalone tag
rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
然后把結(jié)果交給ret變量,基于ret的值又進行劃分按照單標簽和復雜標簽分開處理ret值存在那就是匹配到了單標簽然后再根據(jù)context是不是普通對象又分為兩種情況isPlainObject是檢測是不是普通對象的方法,如果是普通對象,就利用js原生方法createElement傳入標簽創(chuàng)建元素并放在一個數(shù)組里面,之所以這樣是為了以后跟jquery對象合并方便,然后把數(shù)組賦值給selector,后采用對象冒充的方法調(diào)用attr方法,這里attr居然有3個參數(shù),而平常我們使用的api里面是兩個參數(shù),其實jQuery中有很多類似的情況,同樣的方法有著對內(nèi)對外兩個接口。第二個參數(shù)就是對象形式的上下文,因為attr可以像
$("img").attr({ src: "test.jpg", alt: "Test Image" });
這給我們的其實就是我們以后可以$(‘<div>',{id:'div'})這樣寫了也是支持的。如果不是對象就直接創(chuàng)建元素不考慮屬性。還是把創(chuàng)建的元素放在數(shù)組里面。如果ret沒有值那就是復雜的標簽了比如$(‘<div>231</div>')這樣的這個時候原生的js就搞不定啦需要調(diào)取另外一個方法jQuery.buildFragment來處理,這個方法實現(xiàn)以后在學習吧,總之最后都會創(chuàng)建dom元素。最后返回合并后的結(jié)果
return jQuery.merge( this, selector );
不像之前的return this這里是返回merge執(zhí)行后的結(jié)果其實他的任務就是把放在數(shù)組里面的創(chuàng)建好的的dom元素合并到jquery元素中去,最終變成{0:div,length:1...}這樣的對象形式。這樣的話簡標簽情況就處理完畢。
然后else里面處理的是id的情況
elem = document.getElementById( match[2] ); // Check parentNode to catch when Blackberry 4.6 returns // nodes that are no longer in the document #6963 if ( elem && elem.parentNode ) { // Handle the case where IE and Opera return items // by name instead of ID if ( elem.id !== match[2] ) { return rootjQuery.find( selector ); } // Otherwise, we inject the element directly into the jQuery object this.length = 1; this[0] = elem; } this.context = document; this.selector = selector; return this;
很簡單直接調(diào)用原生js的id選擇器但是有一些系統(tǒng)會出現(xiàn)bug
注釋說的很清楚黑莓系統(tǒng),就是元素已經(jīng)不存在了但是依然能夠匹配得到所以再加上父節(jié)點,不存在的元素肯定沒有父節(jié)點的。還有一種情況就是ie和opera瀏覽器會出現(xiàn)按name值匹配的情況所以在做了一個判斷
if ( elem.id !== match[2] ) {
如果真的不幸出現(xiàn)了那就不能使用原生方法而是用find方法也就是使用sizzle引擎了,在大多數(shù)正常情況下就直接將獲取到的元素放到this里面就可以啦然后修改下context的值。Ok終于把第一個大分支分析完了。然后再看根據(jù)match的第二個分支
else if ( !context || context.jquery ) { return ( context || rootjQuery ).find( selector ); }
這里是如果沒有上下文或者上下文是jquery對象的時候這個比較簡單就是直接用find方法了rootjQuery 就是$(document)
最后字符串的情況上面都不屬于的話
return this.constructor( context ).find( selector );
This.constructor就是jQuery其實還是使用find方法。
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關文章
jquery統(tǒng)計用戶選中的復選框的個數(shù)
使用選擇器得到所有被勾選的復選框元素的集合,然后通過判斷元素的個數(shù)來得到用戶勾選的個數(shù),需要的朋友可以參考下2014-06-06jQuery操作checkbox選擇(list/table)
本文將介紹下checkbox list選擇及checkbox table選中附效果圖,感興趣的朋友可以參考下哈,希望可以幫助到你2013-04-04jQuery EasyUI 菜單與按鈕之創(chuàng)建簡單的菜單和鏈接按鈕
這篇文章主要介紹了jQuery EasyUI 菜單與按鈕之創(chuàng)建簡單的菜單和鏈接按鈕,需要的朋友可以參考下2015-11-11