【jQuery源码】select方法
生活随笔
收集整理的這篇文章主要介紹了
【jQuery源码】select方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 /**
2 * select方法是Sizzle選擇器包的核心方法之一,其主要完成下列任務:
3 * 1、調用tokenize方法完成對選擇器的解析
4 * 2、對于沒有初始集合(即seed沒有賦值)且是單一塊選擇器(即選擇器字符串中沒有逗號),
5 * 完成下列事項:
6 * 1) 對于首選擇器是ID類型且context是document的,則直接獲取對象替代傳入的context對象
7 * 2) 若選擇器是單一選擇器,且是id、class、tag類型的,則直接獲取并返回匹配的DOM元素
8 * 3) 獲取最后一個id、class、tag類型選擇器的匹配DOM元素賦值給初始集合(即seed變量)
9 * 3、通過調用compile方法獲取“預編譯”代碼并執行,獲取并返回匹配的DOM元素
10 *
11 * @param selector 已去掉頭尾空白的選擇器字符串
12 * @param context 執行匹配的最初的上下文(即DOM元素集合)。若context沒有賦值,則取document。
13 * @param results 已匹配出的部分最終結果。若results沒有賦值,則賦予空數組。
14 * @param seed 初始集合
15 */
16 select = Sizzle.select = function( selector, context, results, seed ) {
17 var i, tokens, token, type, find,
18 compiled = typeof selector === "function" && selector,
19 //在沒有seed的時候,調用tokensize方法進行詞法解析,結果放在match中
20 //seed - 種子合集(搜索器搜到符合條件的標簽),放入到這個初始集合seed中
21 match = !seed && tokenize( (selector = compiled.selector || selector) );
22
23 results = results || [];
24
25 // Try to minimize operations if there is no seed and only one group
26 //只有一組選擇器,也就是選擇器中沒有逗號
27 if ( match.length === 1 ) {
28
29 // Take a shortcut and set the context if the root selector is an ID
30 //創建一個新的集合賦給tokens,確保原有集合不被更改
31 tokens = match[0] = match[0].slice( 0 );
32 /*
33 * 若選擇器是以id類型開始,且第二個是關系符(即+~>或空格),
34 * 則獲取id所屬對象作為context繼續完成后續的匹配
35 *
36 * 此處的條件判斷依次為:
37 * tokens.length > 2 :若tokens有兩個以上的選擇器
38 * (token = tokens[0]).type === "ID" :第一個選擇器的類型為ID(即以#開頭的),
39 * support.getById :支持getElementById函數
40 * context.nodeType === 9 :context對象是document
41 * documentIsHTML :當前處理的是HTML代碼
42 * Expr.relative[tokens[1].type] :第二個tokens元素是一個關系(即+~>或空格)
43 * 在滿足上面所有條件的情況下,執行if內的語句體
44 */
45 if ( tokens.length > 2 && (token = tokens[0]).type === "ID" &&
46 support.getById && context.nodeType === 9 && documentIsHTML &&
47 Expr.relative[ tokens[1].type ] ) {
48
49 //查找id為matches[1]的元素
50 context = ( Expr.find["ID"]( token.matches[0].replace(runescape, funescape), context ) || [] )[0];
51 if ( !context ) {
52 return results;
53
54 // Precompiled matchers will still verify ancestry, so step up a level
55 } else if ( compiled ) {
56 context = context.parentNode;
57 }
58
59 //去掉selector中第一個選擇器
60 selector = selector.slice( tokens.shift().value.length );
61 }
62
63 // Fetch a seed set for right-to-left matching
64 //其中: "needsContext"= new RegExp( "^" + whitespace + "*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\(" + whitespace + "*((?:-\\d)?\\d*)" + whitespace + "*\\)|)(?=[^-]|$)", "i" )
65 //即是表示如果沒有一些結構偽類,這些是需要用另一種方式過濾,在之后文章再詳細剖析。
66 //那么就從最后一條規則開始,先找出seed集合
67 i = matchExpr["needsContext"].test( selector ) ? 0 : tokens.length;
68 while ( i-- ) {
69 token = tokens[i];
70
71 // Abort if we hit a combinator
72 //如果遇到了關系選擇器則終止循環,> + ~ 空
73 if ( Expr.relative[ (type = token.type) ] ) {
74 break;
75 }
76 /*
77 先看看有沒有搜索器find,搜索器就是瀏覽器一些原生的取DOM接口,簡單的表述就是以下對象了
78 Expr.find = {
79 'ID' : context.getElementById,
80 'CLASS' : context.getElementsByClassName,
81 'NAME' : context.getElementsByName,
82 'TAG' : context.getElementsByTagName
83 }
84 */
85 if ( (find = Expr.find[ type ]) ) {
86 // Search, expanding context for leading sibling combinators
87 // 嘗試一下能否通過這個搜索器搜到符合條件的初始集合seed
88 if ( (seed = find(
89 token.matches[0].replace( runescape, funescape ),
90 rsibling.test( tokens[0].type ) && testContext( context.parentNode ) || context
91 )) ) {
92
93 // If seed is empty or no tokens remain, we can return early
94 //刪除這條標簽
95 tokens.splice( i, 1 );
96 selector = seed.length && toSelector( tokens );
97 //若當前選擇器是否為空,把seed推入results,返回結果
98 if ( !selector ) {
99 push.apply( results, seed );
100 return results;
101 }
102 //已經找到了符合條件的seed集合,此時前邊還有其他規則,跳出去
103 break;
104 }
105 }
106 }
107 }
108
109 // Compile and execute a filtering function if one is not provided
110 // Provide `match` to avoid retokenization if we modified the selector above
111 ( compiled || compile( selector, match ) )(
112 seed,
113 context,
114 !documentIsHTML,
115 results,
116 rsibling.test( selector ) && testContext( context.parentNode ) || context
117 );
118 return results;
119 };
?
轉載于:https://www.cnblogs.com/shytong/p/5351656.html
總結
以上是生活随笔為你收集整理的【jQuery源码】select方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【BZOJ1623】 [Usaco200
- 下一篇: POJ3185 The Water Bo