zoukankan      html  css  js  c++  java
  • jQuery学习-事件之绑定事件(七)

    今天来说说事件中的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 f]*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:([x20 f]*((?:-d)?d*)[x20 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 */ 

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

  • 相关阅读:
    ubuntu下使用golang、qml与ubuntu sdk开发桌面应用 (简单示例)
    Go Revel 学习指南
    Go Revel
    Go Revel
    Go Revel
    Go Revel
    Go Revel
    Go Revel
    Go Revel
    Go Revel
  • 原文地址:https://www.cnblogs.com/urols-jiang/p/4345752.html
Copyright © 2011-2022 走看看