博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

jQuery1.3新特性深度分析(Event)

Posted on 2009-07-27 22:54  linFen  阅读(576)  评论(0编辑  收藏  举报

.3版本的新特性,来看看究竟有什么用处

jQuery.Event = function( src ){
    // Allow instantiation without the 'new' keyword
    // 允许初始化实例不需要new
    if( !this.preventDefault )
        return new jQuery.Event(src);
   
    // Event object
    // 事件对象存在
    if( src && src.type ){
        this.originalEvent = src;
        this.type = src.type;
        this.timeStamp = src.timeStamp;
    // Event type
    }else
        this.type = src;

    if( !this.timeStamp )
        this.timeStamp = now();
   
    // Mark it as fixed
    this[expando] = true;
};

function returnFalse(){
    return false;
}
function returnTrue(){
    return true;
}

// jQuery.Event is based on DOM3 Events as specified by the ECMAScript Language Binding
// http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
//jQuery.Event符合w3c的DOM3标准
jQuery.Event.prototype = {
    //定义preventDefault取消事件的默认动作
    preventDefault: function() {
        this.isDefaultPrevented = returnTrue;

        var e = this.originalEvent;
        if( !e )
            return;
        // if preventDefault exists run it on the original event
        if (e.preventDefault)
            e.preventDefault();
        // otherwise set the returnValue property of the original event to false (IE)
        e.returnValue = false;
    },
    //阻止事件冒泡
    stopPropagation: function() {
        this.isPropagationStopped = returnTrue;

        var e = this.originalEvent;
        if( !e )
            return;
        // if stopPropagation exists run it on the original event
        if (e.stopPropagation)
            e.stopPropagation();
        // otherwise set the cancelBubble property of the original event to true (IE)
        e.cancelBubble = true;
    },
   //可以阻止掉同一事件的其他优先级较低的侦听器的处理
    stopImmediatePropagation:function(){
        this.isImmediatePropagationStopped = returnTrue;
        this.stopPropagation();
    },
    isDefaultPrevented: returnFalse,
    isPropagationStopped: returnFalse,
    isImmediatePropagationStopped: returnFalse
};