分类:事件对象


jQuery's event system normalizes the event object according to W3C standards. The event object is guaranteed to be passed to the event handler. Most properties from the original event are copied over and normalized to the new event object.

jQuery.Event Constructor

The jQuery.Event constructor is exposed and can be used when calling trigger. The new operator is optional.

Check trigger's documentation to see how to combine it with your own event object.

Example:

1
2
3
4
5
//Create a new jQuery.Event object without the "new" operator.
var e = jQuery.Event( "click" );
// trigger an artificial click event
jQuery( "body" ).trigger( e );

As of jQuery 1.6, you can also pass an object to jQuery.Event() and its properties will be set on the newly created Event object.

Example:

1
2
3
4
5
// Create a new jQuery.Event object with specified event properties.
var e = jQuery.Event( "keydown", { keyCode: 64 } );
// trigger an artificial keydown event with keyCode 64
jQuery( "body" ).trigger( e );

Event Properties

jQuery normalizes the following properties for cross-browser consistency:

  • target
  • relatedTarget
  • pageX
  • pageY
  • which
  • metaKey

The following properties are also copied to the event object, though some of their values may be undefined depending on the event:

altKey, bubbles, button, cancelable, charCode, clientX, clientY, ctrlKey, currentTarget, data, detail, eventPhase, metaKey, offsetX, offsetY, originalTarget, pageX, pageY, prevValue, relatedTarget, screenX, screenY, shiftKey, target, view, which

OtherProperties

Certain events may have properties specific to them. Those can be accessed as properties of the event.originalEvent object.

Example:

1
2
3
// add the dataTransfer property for use with the native `drop` event
// to capture information about files dropped into the browser window
jQuery.event.props.push( "dataTransfer" );

event.data

当当前正在执行的处理程序绑定时,一个可选的数据对象传递给一个事件方法。

event.namespace

当事件被触发时此属性包含指定的命名空间。

event.pageX

鼠标相对于文档的左边缘的位置(左边)。

event.pageY

鼠标相对于文档的顶部边缘的位置(坐标)。

event.result

事件被触发的一个事件处理程序的最后返回值,除非值是 undefined。

event.stopPropagation()

防止事件冒泡到DOM树上,也就是不触发的任何前辈元素上的事件处理函数。

event.timeStamp

这个属性返回事件触发时距离1970年1月1日的毫秒数。

event.which

针对键盘和鼠标事件,这个属性能确定你到底按的是哪个键。