jQuery选择器源码解读(八):addCombinator函数
functionaddCombinator(matcher,combinator,base)
1、源码
functionaddCombinator(matcher,combinator,base){ vardir=combinator.dir,checkNonElements=base &&dir==="parentNode",doneName=done++;
returncombinator.first? //Checkagainstclosestancestor/precedingelement function(elem,context,xml){ while((elem=elem[dir])){ if(elem.nodeType===1||checkNonElements){ returnmatcher(elem,context,xml); } } }:
//Checkagainstallancestor/precedingelements function(elem,context,xml){ vardata,cache,outerCache,dirkey=dirruns+""+doneName;
//Wecan'tsetarbitrarydataonXMLnodes,sotheydon't //benefitfromdircaching if(xml){ while((elem=elem[dir])){ if(elem.nodeType===1||checkNonElements){ if(matcher(elem,context,xml)){ returntrue; } } } }else{ while((elem=elem[dir])){ if(elem.nodeType===1||checkNonElements){ outerCache=elem[expando]||(elem[expando]={}); if((cache=outerCache[dir]) &&cache[0]===dirkey){ if((data=cache[1])===true ||data===cachedruns){ returndata===true; } }else{ cache=outerCache[dir]=[dirkey]; cache[1]=matcher(elem,context,xml) ||cachedruns; if(cache[1]===true){ returntrue; } } } } } }; }