0

我读到which所有 jQuery 事件中的参数都是标准化的(在此处指定)。但是,我试图弄清楚(jQuery 的)源代码中的哪个位置实现了这种规范化。我有兴趣弄清楚它是如何完成的。有人有想法吗?您能否指出我正确的方向或在这里发表解释?

4

1 回答 1

1

检查和的代码keyHooksmouseHooks

keyHooks: {
    props: "char charCode key keyCode".split(" "),
    filter: function( event, original ) {

        // Add which for key events
        if ( event.which == null ) {
            event.which = original.charCode != null ? original.charCode : original.keyCode;
        }

        return event;
    }
},

mouseHooks: {
    props: "button buttons clientX clientY fromElement offsetX offsetY pageX pageY screenX screenY toElement".split(" "),
    filter: function( event, original ) {
        var eventDoc, doc, body,
            button = original.button,
            fromElement = original.fromElement;

        // Calculate pageX/Y if missing and clientX/Y available
        if ( event.pageX == null && original.clientX != null ) {
            eventDoc = event.target.ownerDocument || document;
            doc = eventDoc.documentElement;
            body = eventDoc.body;

            event.pageX = original.clientX + ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) - ( doc && doc.clientLeft || body && body.clientLeft || 0 );
            event.pageY = original.clientY + ( doc && doc.scrollTop  || body && body.scrollTop  || 0 ) - ( doc && doc.clientTop  || body && body.clientTop  || 0 );
        }

        // Add relatedTarget, if necessary
        if ( !event.relatedTarget && fromElement ) {
            event.relatedTarget = fromElement === event.target ? original.toElement : fromElement;
        }

        // Add which for click: 1 === left; 2 === middle; 3 === right
        // Note: button is not normalized, so don't use it
        if ( !event.which && button !== undefined ) {
            event.which = ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) );
        }

        return event;
    }
},
于 2012-11-01T10:21:23.300 回答