jQuery選擇器源碼解讀(二):select方法
更新時間:2015年03月31日 10:07:41 作者:mole
這篇文章主要介紹了jQuery選擇器源碼解讀(二):select方法,本文用詳細的注釋解讀了select方法的實現(xiàn)源碼,需要的朋友可以參考下
/*
* select方法是Sizzle選擇器包的核心方法之一,其主要完成下列任務:
* 1、調用tokenize方法完成對選擇器的解析
* 2、對于沒有初始集合(即seed沒有賦值)且是單一塊選擇器(即選擇器字符串中沒有逗號),
* 完成下列事項:
* 1) 對于首選擇器是ID類型且context是document的,則直接獲取對象替代傳入的context對象
* 2) 若選擇器是單一選擇器,且是id、class、tag類型的,則直接獲取并返回匹配的DOM元素
* 3) 獲取最后一個id、class、tag類型選擇器的匹配DOM元素賦值給初始集合(即seed變量)
* 3、通過調用compile方法獲取“預編譯”代碼并執(zhí)行,獲取并返回匹配的DOM元素
*
* @param selector 已去掉頭尾空白的選擇器字符串
* @param context 執(zhí)行匹配的最初的上下文(即DOM元素集合)。若context沒有賦值,則取document。
* @param results 已匹配出的部分最終結果。若results沒有賦值,則賦予空數(shù)組。
* @param seed 初始集合
*/
function select(selector, context, results, seed) {
var i, tokens, token, type, find,
// 調用tokenize函數(shù)解析selector
match = tokenize(selector);
// 若沒有提供初始集合
if (!seed) {
// Try to minimize operations if there is only one group
// 若只有一組選擇器,即選擇器字符串沒有逗號
if (match.length === 1) {
// Take a shortcut and set the context if the root selector
// is an ID
/*
* 下面代碼是用來處理根選擇器是ID類型的快捷方式
*
* 在此使用slice[0]來創(chuàng)建一個新的集合,
* 確保原有的集合不會被之后代碼變更掉
*/
tokens = match[0] = match[0].slice(0);
/*
* 若選擇器是以id類型開始,且第二個是關系符(即+~>或空格),
* 則獲取id所屬對象作為context繼續(xù)完成后續(xù)的匹配
*
* 此處的條件判斷依次為:
* tokens.length > 2 :若tokens有兩個以上的選擇器
* (token = tokens[0]).type === "ID" :第一個選擇器的類型為ID(即以#開頭的),
* support.getById :支持getElementById函數(shù)
* context.nodeType === 9 :context對象是document
* documentIsHTML :當前處理的是HTML代碼
* Expr.relative[tokens[1].type] :第二個tokens元素是一個關系(即+~>或空格)
* 在滿足上面所有條件的情況下,執(zhí)行if內的語句體
*/
if (tokens.length > 2 && (token = tokens[0]).type === "ID"
&& support.getById && context.nodeType === 9
&& documentIsHTML && Expr.relative[tokens[1].type]) {
// 將當前上下文指向第一個ID選擇器指定的節(jié)點對象
context = (Expr.find["ID"](token.matches[0].replace(
runescape, funescape), context) || [])[0];
// 若當前上下文內沒有指定ID對象,則直接返回results
if (!context) {
return results;
}
// 選擇器字符串去掉第一個ID選擇器
selector = selector.slice(tokens.shift().value.length);
}
// Fetch a seed set for right-to-left matching
/*
* 下面while循環(huán)的作用是用來根據(jù)最后一個id、class、tag類型的選擇器獲取初始集合
* 舉個簡單例子:若選擇器是"div[title='2']",
* 代碼根據(jù)div獲取出所有的context下的div節(jié)點,并把這個集合賦給seed變量,
* 然后在調用compile函數(shù),產(chǎn)生預編譯代碼,
* 預編譯代碼完成在上述初始集合中執(zhí)行[title='2']的匹配
*
* 首先,檢查選擇器字符串中是否存在與needsContext正則表達式相匹配的字符
* 若沒有,則將依據(jù)選擇器從右到左過濾DOM節(jié)點
* 否則,將先生成預編譯代碼后執(zhí)行(調用compile方法)。
*/
/*
* "needsContext" : new RegExp("^" + whitespace
* + "*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\("
* + whitespace + "*((?:-\\d)?\\d*)" + whitespace
* + "*\\)|)(?=[^-]|$)", "i")
* needsContext用來匹配選擇器字符串中是否包含下列內容:
* 1、>+~三種關系符
* 2、:even、:odd、:eq、:gt、:lt、:nth、:first、:last八種偽類
* 其中,(?=[^-]|$)用來過濾掉類似于:first-child等帶中杠的且以上述八個單詞開頭的其它選擇器
*/
i = matchExpr["needsContext"].test(selector) ? 0
: tokens.length;
while (i--) {
token = tokens[i];
// Abort if we hit a combinator
// 遇到關系符跳出循環(huán)
if (Expr.relative[(type = token.type)]) {
break;
}
if ((find = Expr.find[type])) {
// Search, expanding context for leading sibling
// combinators
/*
* rsibling = new RegExp(whitespace + "*[+~]")
* rsibling用于判定token選擇器是否是兄弟關系符
*/
if ((seed = find(token.matches[0].replace(
runescape, funescape), rsibling
.test(tokens[0].type)
&& context.parentNode || context))) {
// If seed is empty or no tokens remain, we can
// return early
// 剔除剛用過的選擇器
tokens.splice(i, 1);
selector = seed.length && toSelector(tokens);
/*
* 若selector為空,說明選擇器僅為單一id、class、tag類型的,
* 故直接返回獲取的結果,否則,在獲取seed的基礎上繼續(xù)匹配
*/
if (!selector) {
push.apply(results, seed);
return results;
}
break;
}
}
}
}
}
// Compile and execute a filtering function
// Provide `match` to avoid retokenization if we modified the
// selector above
/*
* 先執(zhí)行compile(selector, match),它會返回一個“預編譯”函數(shù),
* 然后調用該函數(shù)獲取最后匹配結果
*/
compile(selector, match)(seed, context, !documentIsHTML, results,
rsibling.test(selector));
return results;
}
您可能感興趣的文章:
相關文章
為jquery的ajaxfileupload增加附加參數(shù)的方法
這篇文章主要介紹了為jquery的ajaxfileupload增加附加參數(shù)的方法,需要的朋友可以參考下2014-03-03
jquery控制頁面的展開和隱藏實現(xiàn)方法(推薦)
下面小編就為大家?guī)硪黄猨query控制頁面的展開和隱藏實現(xiàn)方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
jQuery實現(xiàn)倒計時重新發(fā)送短信驗證碼功能示例
這篇文章主要介紹了jQuery實現(xiàn)倒計時重新發(fā)送短信驗證碼功能,結合實例形式分析了基于jQuery的倒計時操作功能實現(xiàn)方法,涉及jQuery表單提交、驗證、正則操作等技巧,需要的朋友可以參考下2017-01-01
jquery如何判斷表格同一列不同行input數(shù)據(jù)是否重復
這篇文章主要介紹了jquery如何判斷表格同一列不同行input數(shù)據(jù)是否重復,需要的朋友可以參考下2014-05-05
基于jquery的has()方法以及與find()方法以及filter()方法的區(qū)別詳解
本篇文章介紹了,基于jquery的has()方法以及與find()方法以及filter()方法的區(qū)別詳解需要的朋友參考下2013-04-04
jQuery實現(xiàn)拖動調整表格單元格大小的代碼實例
這篇文章主要介紹了jQuery實現(xiàn)拖動調整表格單元格大小的代碼實例,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-01-01

