我们都知道jQuery的事件其思想来源于Dean Edwards的addEvent,通过源码我们知道jQuery在为元素绑定事件时,每种类型的事件(click,blur)时只绑定了一次对应类型的事件处理方法,实际的方法是存在jQuery的缓存系统中的,这样做的好处我就不多说了,绑定方法的函数为add方法,在执行事件的时,通过handers在缓存系统中获取事件列表,然后通过dispatch函数来执行对应的事件。

jQuery.event = { 
     add: function( elem, types, handler, data, selector ) {                 var tmp, events, t, handleObjIn,             special, eventHandle, handleObj,             handlers, type, namespaces, origType,             elemData = jQuery._data( elem );//从缓存系统中获取对应的事件数据         // 如果不存在事件数据则直接退出         if ( !elemData ) {             return;         }         // Caller can pass in an object of custom data in lieu of the handler         if ( handler.handler ) {             handleObjIn = handler;             handler = handleObjIn.handler;             selector = handleObjIn.selector;         }         // 为事件添加标识         if ( !handler.guid ) {             handler.guid = jQuery.guid++;         }         //初始化事件对象数据         if ( !(events = elemData.events) ) {             events = elemData.events = {};         }                 //每种事件类型只绑定一次事件(eventHanle)         if ( !(eventHandle = elemData.handle) ) {             eventHandle = elemData.handle = function( e ) {                 // Discard the second event of a jQuery.event.trigger() and                 // when an event is called after a page has unloaded                 return typeof jQuery !== strundefined && (!e || jQuery.event.triggered !== e.type) ?                 //通过dispatch来触发事件的执行,eventHandle.elem来绑定当前元素,防止this指向错误(attachEvent中this会指向window的bug)                     jQuery.event.dispatch.apply( eventHandle.elem, arguments ) :                     undefined;             };             // Add elem as a property of the handle fn to prevent a memory leak with IE non-native events             eventHandle.elem = elem;         }         //多个事件的绑定         // Handle multiple events separated by a space         types = ( types || "" ).match( rnotwhite ) || [ "" ];         t = types.length;         while ( t-- ) {             tmp = rtypenamespace.exec( types[t] ) || [];             type = origType = tmp[1];             namespaces = ( tmp[2] || "" ).split( "." ).sort();             // There *must* be a type, no attaching namespace-only handlers             if ( !type ) {                 continue;             }             // If event changes its type, use the special event handlers for the changed type             special = jQuery.event.special[ type ] || {};             // If selector defined, determine special event api type, otherwise given type             type = ( selector ? special.delegateType : special.bindType ) || type;             // Update special based on newly reset type             special = jQuery.event.special[ type ] || {};             //将事件组合成一个事件对象             handleObj = jQuery.extend({                 type: type,                 origType: origType,                 data: data,                 handler: handler,                 guid: handler.guid,                 selector: selector,                 needsContext: selector && jQuery.expr.match.needsContext.test( selector ),                 namespace: namespaces.join(".")             }, handleObjIn );             //handlers对应事件类型的事件列表             if ( !(handlers = events[ type ]) ) {                 handlers = events[ type ] = [];                 handlers.delegateCount = 0;                 // Only use addEventListener/attachEvent if the special events handler returns false                 if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) {                     //真正绑定事件的地方,只绑定eventHandle                     if ( elem.addEventListener ) {                         elem.addEventListener( type, eventHandle, false );                     } else if ( elem.attachEvent ) {                         elem.attachEvent( "on" + type, eventHandle );                     }                 }             }             //特殊事件的处理             if ( special.add ) {                 special.add.call( elem, handleObj );                 if ( !handleObj.handler.guid ) {                     handleObj.handler.guid = handler.guid;                 }             }             // 将事件加入事件列表中             if ( selector ) {                 handlers.splice( handlers.delegateCount++, 0, handleObj );             } else {                 handlers.push( handleObj );             }             //表示事件曾经使用过,用于事件优化             jQuery.event.global[ type ] = true;         }         // 防止ie内存溢出         elem = null;      },      remove: function( elem, types, handler, selector, mappedTypes ) {           //为元素移除事件      },      trigger: function( event, data, elem, onlyHandlers ) {          //执行事件      },      dispatch: function( event ) {          //分配事件(在执行方法时执行)      },      handlers: function( event, handlers ) {          //获取缓存系统中对应事件类型的事件列表      } } //其实我们为每种事件绑定的方式是这样的 //通过dispatch来执行对应的事件 function ( e ) {      // Discard the second event of a jQuery.event.trigger() and      // when an event is called after a page has unloaded      return typeof jQuery !== strundefined && (!e || jQuery.event.triggered !== e.type) ?           jQuery.event.dispatch.apply( eventHandle.elem, arguments ) :       undefined; }

好了,jQuery的绑定函数原理先介绍到这里。下次继续!一天一点,滴水汇河!

jQuery学习-事件之绑定事件(一)的更多相关文章

  1. jQuery学习-事件之绑定事件(三)

    在上一篇<jQuery学习-事件之绑定事件(二)>我们了解了jQuery的dispatch方法,今天我们来学习下handlers 方法: handlers: function( event ...

  2. jQuery学习-事件之绑定事件(二)

    在上一篇<jQuery学习-事件之绑定事件(一)>我们了解了jQuery的add方法,今天我们来学习下dispatch方法: dispatch: function( event ) {   ...

  3. jQuery如何给body绑定事件?

    jQuery如何给body绑定事件? 代码如下: $(document).bind("resize", function () { alert("php-note.com ...

  4. JQuery在循环中绑定事件的问题详解

    JQuery在循环中绑定事件的问题详解 有个页面上需要N个DOM,每个DOM里面的元素ID都要以数字结尾,比如说 ? 1 2 3 <input type="text" nam ...

  5. jquery学习笔记(三):事件和应用

    内容来自[汇智网]jquery学习课程 3.1 页面加载事件 在jQuery中页面加载事件是ready().ready()事件类似于就JavaScript中的onLoad()事件,但前者只要页面的DO ...

  6. jQuery学习小结1-CSS操作+事件

    一.DOM对象和jQuery 对象互换 1.jQuery对象 就是通过jQuery包装DOM对象后产生的对象.jQuery对象是jQuery独有的,其可以使用jQuery里的方法.比如: $(&quo ...

  7. [jquery]高级篇--js绑定事件

    参考:  http://www.cnblogs.com/leejersey/p/3545372.html jQuery on()方法是官方推荐的绑定事件的一个方法.$(selector).on(eve ...

  8. jquery html 动态添加元素绑定事件

    由于实际的需要,有时需要往网页中动态的插入HTML内容,并在插入的节点中绑定事件处理函数.我们知道,用Javascript向HTML文档中 插入内容,有两种方法, 一种是在写HTML代码写入JS,然后 ...

  9. jquery事件与绑定事件

    1.首先,我们来看一下经常使用的添加事件的方式: <input type="button" id="btn" value="click me!& ...

随机推荐

  1. 常用网站开发类Firefox扩展插件 (转)

    作为一个 Web 开发人员,你几乎没有理由不喜欢Firefox,因为在Firefox下有很多专门针对开发的扩展插件,非常好用,这里就介绍一些常用的针对网站开发的FireFox扩展,供Web开发人员参考 ...

  2. PowerDesigner中NAME和COMMENT的互相转换,需要执行语句

    原文: http://www.cnblogs.com/xnxylf/p/3288718.html 由于PDM 的表中 Name 会默认=Code 所以很不方便, 所以需要将 StereoType 显示 ...

  3. Python中各种集合 list tuple set dict

    list 创建list        L = ['Adam','Lucy','Bart'] 索引访问:  正序(和数组类似)     L[0],L[1],L[2] 倒序 L[-1]倒数第一个  L[- ...

  4. MFC socket网络通讯核心代码

    服务器: AfxSocketInit();//初始化,必须执行这个函数socket才能正常执行 server.Create(10086); server.Listen(10); while(1) { ...

  5. linux之SQL语句简明教程

    本教程参考http://www.1keydata.com/cn/sql/ 目的是让初学者了解linux下Mysql的操作,但是我仍想侧重于SQL语句的讲解 sql语句的学习将按照下图的流程: 当然在这 ...

  6. OC中如何把字典中的数据拼接成url字符串

    在使用objective-c语言开发iOS应用中,会向服务器通过URL请求一些数据,因此对URL的拼接肯定少不了.而在iOS中,我们一般是通过将字典中的数据拼接成我们要请求的URL字符串,那这个是怎么 ...

  7. Appium for Windows环境搭建

    服务环境: 1.安装Nodejs 下载nodejs安装包(http://nodejs.org/download/)安装 测试安装是否成功:运行cmd,输入node -v 2.安装android的SKD ...

  8. Android EditText限制输入一些固定字符的属性

    android:digits="abcdefghijklmnopqrstuvwxyz1234567890" 仅仅能输入这些

  9. 【LeetCode】【Python】Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  10. JS获取客户端IP地址、MAC和主机名七种方法

    一.使用JS获取客户端IP的几个方法方法一(只针对IE且客户端的IE允许AcitiveX运行,通过平台:XP,SERVER03,2000).获取客户端IP代码:<HTML><HEAD ...