首先需要明白,瀏覽器的原生事件是只讀的,限制了jQuery對(duì)他的操作。舉個(gè)簡(jiǎn)單的例子就能明白為什么jQuery非要構(gòu)造一個(gè)新的事件對(duì)象。
在委托處理中,a節(jié)點(diǎn)委托b節(jié)點(diǎn)在a被click的時(shí)候執(zhí)行fn函數(shù)。當(dāng)事件冒泡到b節(jié)點(diǎn),執(zhí)行fn的時(shí)候上下文環(huán)境需要保證正確,是a節(jié)點(diǎn)執(zhí)行了fn而非b節(jié)點(diǎn)。如何保證執(zhí)行fn的上下文環(huán)境是a節(jié)點(diǎn)的:看源碼(紅色部分)
//執(zhí)行ret = ( (jQuery.event.special[ handleObj.origType ] || {}).handle || handleObj.handler ).apply( matched.elem, args );
使用了apply將執(zhí)行函數(shù)的上下文替換成了a節(jié)點(diǎn)(matched.elem)。還有一點(diǎn)args[0]即是事件對(duì)象event。又如何保證event是a節(jié)點(diǎn)的事件的?這就是event.currentTarget這個(gè)重要的屬性的功能,所以在執(zhí)行apply之前還做了一步操作
event.currentTarget = matched.elem;
直接更改事件對(duì)象的currentTarget屬性,這在瀏覽器本地事件是做不到的。所以才有了基于本地事件構(gòu)造jQuery的事件對(duì)象。
事件分兩種:鼠標(biāo)事件和鍵盤事件(不知道觸摸事件何時(shí)能加進(jìn)來)。看一下這兩者的詳細(xì)屬性


其中有些是瀏覽器自己的,非W3C標(biāo)準(zhǔn)的。jQuery將事件屬性分為三塊
鼠標(biāo)和鍵盤事件共同擁有的屬性jQuery.event. 鍵盤事件專有的屬性jQuery.event.keyHooks.props: "char charCode key keyCode".split(" ") 鼠標(biāo)事件專有的屬性jQuery.event.mouseHooks.props: "button buttons clientX clientY fromElement offsetX offsetY pageX pageY screenX screenY toElement".split(" ") 構(gòu)造新的事件對(duì)象分三步完成 第一步,使用到event = new jQuery.Event( originalEvent ),構(gòu)造新事件對(duì)象(不明白new的作用的請(qǐng)點(diǎn)擊這里),并在創(chuàng)建事件的時(shí)候加上isDefaultPrevented、originalEvent、type 、timeStamp和事件已經(jīng)被修正過的標(biāo)記(優(yōu)化使用,避免不必要的處理)。jQuery.Event(src, props)的源碼如下a. 構(gòu)造新的事件對(duì)象jQuery.event.fix(originalEvent)

jQuery.Event = function( src, props ) { // Allow instantiation without the 'new' keyWord if ( !(this instanceof jQuery.Event) ) { return new jQuery.Event( src, props ); } //src為事件對(duì)象 if ( src && src.type ) { this.originalEvent = src; this.type = src.type; //事件冒泡的文檔可能被標(biāo)記為阻止默認(rèn)事件發(fā)生;這個(gè)函數(shù)可以反應(yīng)是否阻止的標(biāo)志的正確值 this.isDefaultPrevented = ( src.defaultPrevented || src.returnValue === false || src.getPreventDefault && src.getPreventDefault() ) ? returnTrue : returnFalse; //src為事件類型 } else { this.type = src; } //將明確提供的特征添加到事件對(duì)象上 if ( props ) { jQuery.extend( this, props ); } //創(chuàng)建一個(gè)時(shí)間戳如果傳入的事件不只一個(gè) this.timeStamp = src && src.timeStamp || jQuery.now(); //標(biāo)記事件已經(jīng)修正過 this[ jQuery.expando ] = true;};
新聞熱點(diǎn)
疑難解答
圖片精選