今天我们来学习jQuery.Event对象。jQuery为了添加自己的处理机制,及可以传递用户自定义数据,于是Event对象就出世了。

  1 jQuery.Event = function( src, props ) {

  2     //instanceof 用于判断一个变量是否某个对象的实例

  3     if ( !(this instanceof jQuery.Event) ) {

  4         return new jQuery.Event( src, props );

  5     }

  6 

  7     // Event object

  8     if ( src && src.type ) {

  9         /*

 10          如果是event对象

 11          * */

 12         this.originalEvent = src;//将原生的event对象存于Event中

 13         this.type = src.type;//事件类型

 14 

 15         // Events bubbling up the document may have been marked as prevented

 16         // by a handler lower down the tree; reflect the correct value.

 17         /*

 18          修正isDefaultPrevented方法

 19          * */

 20         this.isDefaultPrevented = src.defaultPrevented ||

 21                 src.defaultPrevented === undefined &&

 22                 // Support: IE < 9, Android < 4.0

 23                 src.returnValue === false ?

 24             returnTrue :

 25             returnFalse;

 26 

 27     // Event type

 28     } else {

 29         //如果event是事件名称

 30         this.type = src;

 31     }

 32 

 33     // Put explicitly provided properties onto the event object

 34     //添加自定义属性

 35     if ( props ) {

 36         jQuery.extend( this, props );

 37     }

 38 

 39     // Create a timestamp if incoming event doesn't have one

 40     this.timeStamp = src && src.timeStamp || jQuery.now();//添加时间戳

 41 

 42     // Mark it as fixed

 43     this[ jQuery.expando ] = true;//标识event已被处理过

 44 };

 45 

 46 jQuery.Event.prototype = {

 47     isDefaultPrevented: returnFalse,

 48     isPropagationStopped: returnFalse,

 49     isImmediatePropagationStopped: returnFalse,

 50 

 51     preventDefault: function() {

 52         /*

 53          修正【阻止默认事件】

 54          * */

 55         var e = this.originalEvent;//取出原生evnet

 56 

 57         this.isDefaultPrevented = returnTrue;

 58         if ( !e ) {

 59             return;

 60         }

 61 

 62         // If preventDefault exists, run it on the original event

 63         if ( e.preventDefault ) {

 64             e.preventDefault();

 65 

 66         // Support: IE

 67         // Otherwise set the returnValue property of the original event to false

 68         } else {

 69             e.returnValue = false;

 70         }

 71     },

 72     stopPropagation: function() {

 73         /*

 74          修正【停止冒泡】

 75          * */

 76         var e = this.originalEvent;//取出原生evnet

 77 

 78         this.isPropagationStopped = returnTrue;

 79         if ( !e ) {

 80             return;

 81         }

 82         // If stopPropagation exists, run it on the original event

 83         if ( e.stopPropagation ) {

 84             e.stopPropagation();

 85         }

 86 

 87         // Support: IE

 88         // Set the cancelBubble property of the original event to true

 89         e.cancelBubble = true;

 90     },

 91     stopImmediatePropagation: function() {

 92         /*

 93          修正【stopImmdiatePropagation】

 94          stopImmediatePropagation 的功能比stopPropagation 多一些,

 95          除了可以阻止事件冒泡之外,还可以把这个元素绑定的同类型事件也阻止了。

 96          * */

 97         var e = this.originalEvent;

 98 

 99         this.isImmediatePropagationStopped = returnTrue;

 

         if ( e && e.stopImmediatePropagation ) {

             e.stopImmediatePropagation();

         }

 

         this.stopPropagation();

     }
107 }; 

Event方法主要是对兼容性做了处理看注释应该就明白了!

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

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

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

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

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

  3. jQuery学习-事件之绑定事件(一)

    我们都知道jQuery的事件其思想来源于Dean Edwards的addEvent,通过源码我们知道jQuery在为元素绑定事件时,每种类型的事件(click,blur)时只绑定了一次对应类型的事件处 ...

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

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

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

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

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

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

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

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

  8. jQuery相关方法7----各种事件和绑定事件

    一.jQuery事件 1.鼠标事件 click与dbclick事件 click事件其实是由mousedown与mouseup 2个动作构成,所以点击的动作只有在松手后才触发 $ele.click(): ...

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

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

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

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

随机推荐

  1. AdapterView及其子类之四:基于ListView及SimpleAdapter实现列表

    代码请见SimpleAdapterDemo.zip. 步骤如下: 1.创建主布局文件 <RelativeLayout xmlns:android="http://schemas.and ...

  2. c#接口定义与应用

    public interface IBankAccount //只能加public修饰符,或者什么都不加 { void Playin(decimal money); //函数前不加任何修饰符号 boo ...

  3. python之函数的使用

    备注:本篇文章主要讲一讲函数及集合的一些常用用法: 一.首先先看下,集合(set): 集合的特点:无序.不重复(这点跟字典有点像) <1>,在需要访问集合的时候,由于集合本身是无序的,所以 ...

  4. Lotto--poj2245

    Lotto Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6605   Accepted: 4185 Description ...

  5. Azure上如何在Linux下挂载数据磁盘

    [原文首次发表于51cto http://cloudapps.blog.51cto.com/3136598/1653672] 在Azure上创建了虚拟机之后,我们在一些情况下会需要添加更多的数据磁盘来 ...

  6. Oracle EBS-SQL (QA-3):检查已检验未入库.sql

    DEFINE RECE="%"  SELECT rsh.receipt_num                                    收据号,           ...

  7. VS调试时监视上一个错误代码和错误的文本描述

    以前我都是用GetLastError()然后在MSDN里面查错误原因的.现在才知道有很简便的方法: 在Watch窗口选择一行,然后输入$err,hr

  8. KeyEvent

    http://blog.csdn.net/elfylin/article/details/8008763 一. 接口KeyEvent.Callback和View.OnKeyListener 二. 流程 ...

  9. Android网络编程之Http通信

    Android中提供的HttpURLConnection和HttpClient接口可以用来开发HTTP程序.以下是本人在学习中的总结与归纳.1. HttpURLConnection接口    首先需要 ...

  10. extjs让按钮可用或者不可用

    Ext.getCmp(‘按钮ID’).enable();//设置为可用Ext.getCmp(‘按钮ID’).disable();//设置为不可用