MouseEvent() 构造函数创建新 MouseEvent .

句法

 event = new MouseEvent(typeArg, mouseEventInit);
					

typeArg
DOMString 表示事件的名称。
mouseEventInit 可选
MouseEventInit 字典,拥有以下字段:
  • "screenX" , optional and defaulting to 0 , of type long , that is the horizontal position of the mouse event on the user's screen; setting this value doesn't move the mouse pointer.
  • "screenY" , optional and defaulting to 0 , of type long , that is the vertical position of the mouse event on the user's screen; setting this value doesn't move the mouse pointer.
  • "clientX" , optional and defaulting to 0 , of type long , that is the horizontal position of the mouse event on the client window of user's screen; setting this value doesn't move the mouse pointer.
  • "clientY" , optional and defaulting to 0 , of type long , that is the vertical position of the mouse event on the client window of the user's screen; setting this value doesn't move the mouse pointer.
  • "ctrlKey" , optional and defaulting to false , of type 布尔 , that indicates if the ctrl key was simultaneously pressed.
  • "shiftKey" , optional and defaulting to false , of type 布尔 , that indicates if the shift key was simultaneously pressed.
  • "altKey" , optional and defaulting to false , of type 布尔 , that indicates if the alt key was simultaneously pressed.
  • "metaKey" , optional and defaulting to false , of type 布尔 , that indicates if the meta key was simultaneously pressed.
  • "button" , optional and defaulting to 0 , of type short , that describes which button is pressed during events related to the press or release of a button:
    含义
    0 Main button pressed (usually the left button) or un-initialized
    1 Auxiliary button pressed (usually the middle button)
    2 Secondary button pressed (usually the right button)
  • "buttons" , optional and defaulting to 0 , of type unsigned short , that describes which buttons are pressed when the event is launched:
    位字段值 含义
    0 No button pressed
    1 Main button pressed (usually the left button)
    2 Secondary button pressed (usually the right button)
    4 Auxiliary button pressed (usually the middle button)
  • "relatedTarget" , optional and defaulting to null , of type EventTarget , that is the element just left (in case of  a mouseenter or mouseover ) or is entering (in case of a mouseout or mouseleave ).
  • "region" , optional and defaulting to null , of type DOMString , is the id of the hit region affected by the event. The absence of any hit region is affected, is represented by the null 值。

In some implementations, passing anything other than a number for the screen and client fields will throw a TypeError .

MouseEventInit 字典还接受字段来自 UIEventInit 和来自 EventInit 字典。

规范

规范 状态 注释
CSSOM (CSS 对象模型) 视图模块
在该规范中的 MouseEvent 定义。
工作草案 Redefines screen and client fields long to double.
DOM (文档对象模型) 3 级事件规范
在该规范中的 MouseEvent() 定义。
过时 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
MouseEvent() 构造函数 Chrome 47 Edge ≤79 Firefox 11 IE No Opera Yes Safari Yes WebView Android 47 Chrome Android 47 Firefox Android 14 Opera Android Yes Safari iOS Yes Samsung Internet Android 5.0
Redefined mouseEventInit 字段从 long to double Chrome 56 Edge ≤79 Firefox ? IE No Opera ? Safari ? WebView Android 56 Chrome Android 56 Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android 6.0
支持 mouseEventInit optional region field Chrome 51 Disabled
51 Disabled
Flag needed to retrieve value from MouseEvent.region .
Disabled ). To change preferences in Chrome, visit
Edge ≤79 Disabled
≤79 Disabled
Flag needed to retrieve value from MouseEvent.region .
Disabled From version ≤79: this feature is behind the Experimental Web Platform Features preference (needs to be set to true ).
Firefox 32 IE No Opera ? Safari ? WebView Android No Chrome Android No Firefox Android 32 Opera Android ? Safari iOS ? Samsung Internet Android No

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

Polyfill

You can polyfill the MouseEvent() constructor functionality in Internet Explorer 9 and higher with the following code:

(function (window) {
  try {
    new MouseEvent('test');
    return false; // No need to polyfill
  } catch (e) {
		// Need to polyfill - fall through
  }
    // Polyfills DOM4 MouseEvent
	var MouseEventPolyfill = function (eventType, params) {
		params = params || { bubbles: false, cancelable: false };
		var mouseEvent = document.createEvent('MouseEvent');
		mouseEvent.initMouseEvent(eventType,
			params.bubbles,
			params.cancelable,
			window,
			0,
			params.screenX || 0,
			params.screenY || 0,
			params.clientX || 0,
			params.clientY || 0,
			params.ctrlKey || false,
			params.altKey || false,
			params.shiftKey || false,
			params.metaKey || false,
			params.button || 0,
			params.relatedTarget || null
		);
		return mouseEvent;
	}
	MouseEventPolyfill.prototype = Event.prototype;
	window.MouseEvent = MouseEventPolyfill;
})(window);
							

另请参阅

元数据

  • 最后修改: