今天我们来学习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. DedeCms 建站随笔(一)

    站名:524社区网 代码:DedeCms织梦5.7代码(UTF-8) 服务器:Linux 问题一:火车头(熊猫)采集,登录成功之后无法获取栏目列表. 原因:1.新建栏目后没有更新栏目HTML文件,并且 ...

  2. Leetcode 283 Move Zeroes python

    题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...

  3. HeadFirst设计模式读书笔记(2)-观察者模式(Observer Pattern)

    观察者模式:定义了对象之间一对多的依赖关系,这样一来,当一个对象的状态发生改变时,它的依赖者将会受到通知并且自动更新. 有一个模式可以帮你的对象知悉现况,不会错过该对象感兴趣的事,对象甚至在运行时可以 ...

  4. 载入OpenSSL的动态库——学会使用tryToLoadOpenSslWin32Library和QPair

    Libraries name of openssl? The "library" portion of OpenSSL consists of two libraries. On ...

  5. 13个JavaScript图表(JS图表)图形绘制插件

    转自:http://blog.jobbole.com/13671/ 1. Flash 过去是最佳解决方案,但很多人多在从那迁移: 2. 现代浏览器及其更强大的计算能力,使其在转化绘制实时数据方面的能力 ...

  6. 蓝桥杯之JAM的计数法

    题目描述 Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个数字的位数都是相同的(使用相同个数的字母),英文字 ...

  7. 杭电oj1219 AC Me

    Tips:本题中,输入字符串之后,直接从头到尾处理一遍,调用函数判断是否是字母,不要自己写循环判断是否为字母,易超时! 不过本题中有一个疑问,自己最开始用C写的,一直是Time Limit Excee ...

  8. Ruiy classicsQuotations

    1,IT界,许多人会称自己为菜鸟,而每只菜鸟都会有鹰的梦想; 2,把做十件事精力用来做一件事情,你事业就经典了;

  9. android 自定义圆形进度条

    一.通过动画实现 定义res/anim/loading.xml如下: [html]  view plain copy print ?   <?xml version="1.0" ...

  10. Swift中元组(Tuples),结构体(Struct),枚举(Enums)之间的区别

    Swift有许多种存储数据方式,你可以用枚举(enums),元组(tuples),结构体(structs),类(classes),在这篇文章中我们将比较枚举.元组.结构体之间区别,首先从最简单的开始- ...