码迷,mamicode.com
首页 > Web开发 > 详细

jQuery学习-事件之绑定事件(七)

时间:2015-03-17 23:34:31      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

今天来说说事件中的handlers方法中的一个片段

 1 matches[ sel ] = handleObj.needsContext ?
 2     jQuery( sel, this ).index( cur ) >= 0 :
 3     jQuery.find( sel, thisnull, [ cur ] ).length;
 4 /*
 5  这是handler是方法中过滤委托的方法。等价于
 6 if(handleObj.needsContext){
 7     matches[ sel ] = jQuery( sel, this ).index( cur ) >= 0;
 8        这句语句的意思是:
 9          在this(这里是被绑定委托方法的元素)元素中查找sel(selector)表达式的元素,
10          这里返回的是数组,然后在结果数组中查找cur(触发事件元素),返回其索引值。 
11          如果大于-1为true反之false;
12 }else{
13     matches[ sel ] = jQuery.find( sel, this, null, [ cur ] ).length;
14       这句语句的意思是在this元素中查找符合sel表达式的元素,同时该元素存在与[cur]这个数组中,
15       返回数组的长度。
16 }
17  那handleObj.needsContext又是什么,请接着看下去......
18  handleObj.neddsContext是出现在jQuery事件add方法中的,如下:      
19 */
20 
21 handleObj = jQuery.extend({
22                 type: type,//事件类型名称
23                 origType: origType,//事件类型名称
24                 data: data,//自定数据
25                 handler: handler,//事件
26                 guid: handler.guid,//事件的guid
27                 selector: selector,//委托的selector
28                 needsContext: selector && jQuery.expr.match.needsContext.test( selector ),
29                 /*
30                  jQuery.expr.mathc.needsContext = 
31                      /^[\x20\t\r\n\f]*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\([\x20\t\r\n\f]*((?:-\d)?\d*)[\x20\t\r\n\f]*\)|)(?=[^-]|$)/i
32                  这个判断时判断类似"p:first,p:odd .... p:eq(2),p:last[attr=xxx]等selector"
33                  * * */
34                 namespace: namespaces.join(".")
35             }, handleObjIn );        
36 /*
37 看到上诉注释,是不是很清楚了,needsContext其实就是判断first,odd等这种快捷特殊的表达式的,
38 如果我们的selector="p:last"这样的,就是最后一个P元素符合条件

39 */ 

今天又被吊了,唉~,飘飘飘飘过了

jQuery学习-事件之绑定事件(七)

标签:

原文地址:http://www.cnblogs.com/urols-jiang/p/4345752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!