欢迎来到天天文库
浏览记录
ID:30769488
大小:187.20 KB
页数:10页
时间:2019-01-03
《jquery191源码分析系列(十二)之筛选操作_jquery》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、Jquery1.9.1源码分析系列(十二)之筛选操作废话不多说了直接奔入主题了。jQuery.fn.find(selector)find接受一个参数表达式selector:选择器(字符串)、D0M元素(Element)、jQuery对象。分两种情况处理:第一种,如果传入的参数是非字符串,则先通过jQuery选择器将selector查找出来,然后过滤出包含于当前jQuery对象所匹配的元素的节点。if(typeofselector!==z,string,z){self二this;returnthis.pushStack(jQuery(selector).fi1ter
2、(function(){for(i=0;i3、$(selector,context)变成$(context).find(selector),需要去重和pushStackret二this.pushStack(len>1?jQuery.unique(ret):ret);ret.selector=(this.selector?this.selector+:"")+seiector;rcturnret;jQuery.fn.closest(selectors,context)第二个参数是可选的。函数用于从当前匹配元素开始,逐级向上级选取符合指定表达式的第一个元素,并以jQuery对象的形式返回。这里的表达式包括:选择器4、(字符串)>D0M元素(Element)>jQuery对象。代码中的处理步骤为1.根据传递的参数先查询出结果保存在pos小。pos=rneedsContext.test(selectors)typeofselectors!=="string,z?jQuery(selectors,context5、6、this.context):0;2•遍历当MjQuery对象的每一个元素,从这个元素开始,逐级向上级选取符合指定表达式的第一个祖先元素。for(;i<1;i++){cur=this[i];while(cur&&cur・ownerDocumcnt&&cur!==contex7、t&&cur・nodcTypc!==11){if(pos?pos・index(cur)>-1:jQuery.find.matchesSelector(cur,selectors)){ret.push(cur);break;}cur二cur.parentNode;}}returnthis・pushStack(ret.1ength>1?jQuery.unique(ret):ret);parents()和.closest()方法类似,它们都沿DOM树向上遍历。但区别也很大closest找到第一个符合条件就截止,parents是找到所有符合条件的集合。jQuery.fn.8、parent/parents/parentsUntil/next/prev/nextAll/prevAll/ncxtUntil/prcvUntil/siblings/childrcn/contcnts详解以上几组筛选被放在一起处理,源码如下jQuery.each({parent:function(elem){•••},parents:function(elem){•••},parentsUnt订:function(elem,i,until){•••},next:function(elem){•••},prcv:function(clcm){•••},ncxtAll9、:function(clcm){•••},prevAll:function(elem){•••},i,until)i,until)nextUntil:function(elem,prevUntil:function(elem,siblings:function(elem)childrcn:function(clcm)contents:function(elem)},function(name,fn){jQuery.fn[name]二function(until,selector)varret二jQuery.map(this,fn,until);〃过滤);retur10、nthis
3、$(selector,context)变成$(context).find(selector),需要去重和pushStackret二this.pushStack(len>1?jQuery.unique(ret):ret);ret.selector=(this.selector?this.selector+:"")+seiector;rcturnret;jQuery.fn.closest(selectors,context)第二个参数是可选的。函数用于从当前匹配元素开始,逐级向上级选取符合指定表达式的第一个元素,并以jQuery对象的形式返回。这里的表达式包括:选择器
4、(字符串)>D0M元素(Element)>jQuery对象。代码中的处理步骤为1.根据传递的参数先查询出结果保存在pos小。pos=rneedsContext.test(selectors)typeofselectors!=="string,z?jQuery(selectors,context
5、
6、this.context):0;2•遍历当MjQuery对象的每一个元素,从这个元素开始,逐级向上级选取符合指定表达式的第一个祖先元素。for(;i<1;i++){cur=this[i];while(cur&&cur・ownerDocumcnt&&cur!==contex
7、t&&cur・nodcTypc!==11){if(pos?pos・index(cur)>-1:jQuery.find.matchesSelector(cur,selectors)){ret.push(cur);break;}cur二cur.parentNode;}}returnthis・pushStack(ret.1ength>1?jQuery.unique(ret):ret);parents()和.closest()方法类似,它们都沿DOM树向上遍历。但区别也很大closest找到第一个符合条件就截止,parents是找到所有符合条件的集合。jQuery.fn.
8、parent/parents/parentsUntil/next/prev/nextAll/prevAll/ncxtUntil/prcvUntil/siblings/childrcn/contcnts详解以上几组筛选被放在一起处理,源码如下jQuery.each({parent:function(elem){•••},parents:function(elem){•••},parentsUnt订:function(elem,i,until){•••},next:function(elem){•••},prcv:function(clcm){•••},ncxtAll
9、:function(clcm){•••},prevAll:function(elem){•••},i,until)i,until)nextUntil:function(elem,prevUntil:function(elem,siblings:function(elem)childrcn:function(clcm)contents:function(elem)},function(name,fn){jQuery.fn[name]二function(until,selector)varret二jQuery.map(this,fn,until);〃过滤);retur
10、nthis
此文档下载收益归作者所有