--- title: MouseEvent.initMouseEvent() slug: Web/API/MouseEvent/initMouseEvent translation_of: Web/API/MouseEvent/initMouseEvent ---
{{APIRef("DOM Events")}}{{deprecated_header}}
MouseEvent.initMouseEvent()
方法用以在鼠标事件创建时(一般用 {{domxref("Document.createEvent()")}}方法创建)初始化其属性的值。
事件初始化是在事件被{{ domxref("Document.createEvent()") }}方法创建后必需的。这个方法必须在事件被{{ domxref("EventTarget.dispatchEvent()") }}方法发送出来前调用。一旦事件被发送后,它将不再起任何作用。
不要再用此方法,已过时。
使用特定的事件构造器来替代它,像 {{domxref("MouseEvent.MouseEvent", "MouseEvent()")}}。创建并发送事件 页面里有更多的使用信息。
event.initMouseEvent(type, canBubble, cancelable, view, detail, screenX, screenY, clientX, clientY, ctrlKey, altKey, shiftKey, metaKey, button, relatedTarget);
type
click,
mousedown
,mouseup
,mouseover
,mousemove
,mouseout
。canBubble
cancelable
view
detail
screenX
screenY
clientX
clientY
ctrlKey
altKey
shiftKey
metaKey
button
relatedTarget
mouseover
?和 mouseout
)。其它的传null。
<div style="background:red;width:180px;padding:10px;">
<div id="out"></div>
<input type="text">
</div>
document.body.onclick = function(){ e = arguments[0]; var dt = e.target,stag = dt.tagName.toLowerCase();
document.getElementById("out").innerHTML = stag;}; var simulateClick = function(){ var evt = document.createEvent("MouseEvents"); evt.initMouseEvent("click", true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null); document.body.dispatchEvent(evt); } simulateClick();//Why it can not show "input" ?
这里有个在线演示
{{EmbedLiveSample('Example', 200, 36)}}
{{ LiveSampleLink('Example', 'Link to live demo') }}
Specification | Status | Comment |
---|---|---|
{{SpecName('DOM3 Events', '#idl-interface-MouseEvent-initializers', 'MouseEvent.initMouseEvent()')}} | {{Spec2('DOM3 Events')}} | From {{SpecName('DOM2 Events')}}, deprecated. |
{{SpecName('DOM2 Events', '#Events-Event-initMouseEvent', 'MouseEvent.initMouseEvent()')}} | {{Spec2('DOM2 Events')}} | Initial definition. |
{{ CompatibilityTable() }}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatUnknown() }} |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatUnknown() }} |