我曾经习惯在Javascript中模拟鼠标点击次数:
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, document.view, 1, 1492, 398, 1308, 274, false, false, false, false, 0, null);
var element = document.getElementById("x:1430424820.87:chkState:0");
element.dispatchEvent(evt);
虽然这会调度click事件,但initMouseEvent()并没有设置“按钮”按钮' DOM 3中描述的MouseEvent接口的属性:
[Constructor(DOMString typeArg, optional MouseEventInit mouseEventInitDict)]
interface MouseEvent : UIEvent {
readonly attribute long screenX;
readonly attribute long screenY;
readonly attribute long clientX;
readonly attribute long clientY;
readonly attribute boolean ctrlKey;
readonly attribute boolean shiftKey;
readonly attribute boolean altKey;
readonly attribute boolean metaKey;
readonly attribute short button;
readonly attribute EventTarget? relatedTarget;
// Introduced in DOM Level 3
readonly attribute unsigned short buttons;
boolean getModifierState (DOMString keyArg);
};
添加" evt.buttons = 1" ...
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, document.view, 1, 1492, 398, 1308, 274, false, false, false, false, 0, null);
evt.buttons = 1;
var element = document.getElementById("x:1430424820.87:chkState:0");
element.dispatchEvent(evt);
......没有效果。有谁知道怎么做?
答案 0 :(得分:1)
使用.createEvent()
和initEvent()
(= 现已弃用),您可以使用oldschool方式。您可以在一次调用事件构造函数(每个事件类型都有自己的,例如KeyboardEvent,MouseEvent,...)中创建和初始化事件,如下所示:
var evt = new MouseEvent('click', {
button: 0,
buttons: 1,
bubbles: true,
/* for all available properties see reference below */
});
element.dispatchEvent(evt);
创建一个没有init-object的MouseEvent,以查看属性的默认值:
console.log(new MouseEvent('click'));
答案 1 :(得分:0)
非常感谢你的回应。这比我最终做的要容易得多:
var evtInit = {};
// Event interface
Object.defineProperty(evtInit, 'bubbles', {writable: true, enumerable: true, configurable: true, value: true}, false);
Object.defineProperty(evtInit, 'cancelable', {writable: true, enumerable: true, configurable: true, value: true}, false);
Object.defineProperty(evtInit, 'currentTarget', {writable: true, enumerable: true, configurable: true, value: true}, false);
Object.defineProperty(evtInit, 'eventPhase', {writable: true, enumerable: true, configurable: true, value: true}, false);
Object.defineProperty(evtInit, 'target', {writable: true, enumerable: true, configurable: true, value: true}, false);
Object.defineProperty(evtInit, 'timeStamp', {writable: true, enumerable: true, configurable: true, value: true}, false);
Object.defineProperty(evtInit, 'type', {writable: true, enumerable: true, configurable: true, value: 2}, false);
// UIEvent interface
Object.defineProperty(evtInit, 'view', {writable: true, enumerable: true, configurable: true, value: null}, false);
Object.defineProperty(evtInit, 'detail', {writable: true, enumerable: true, configurable: true, value: 0}, false);
// MouseEvent interface
Object.defineProperty(evtInit, 'button', {writable: true, enumerable: true, configurable: true, value: 0}, false);
Object.defineProperty(evtInit, 'buttons', {writable: true, enumerable: true, configurable: true, value: 0}, false);
Object.defineProperty(evtInit, 'clientX', {writable: true, enumerable: true, configurable: true, value: 0}, false);
Object.defineProperty(evtInit, 'clientY', {writable: true, enumerable: true, configurable: true, value: 0}, false);
Object.defineProperty(evtInit, 'ctrlKey', {writable: true, enumerable: true, configurable: true, value: 0}, false);
Object.defineProperty(evtInit, 'screenX', {writable: true, enumerable: true, configurable: true, value: 0}, false);
Object.defineProperty(evtInit, 'screenY', {writable: true, enumerable: true, configurable: true, value: 0}, false);
// These two don't work. Don't know why.
Object.defineProperty(evtInit, 'mozPressure', {writable: true, enumerable: true, configurable: true, value: 1}, false);
Object.defineProperty(evtInit, 'mozInputSource', {writable: true, enumerable: true, configurable: true, value: MouseEvent.FOO}, true);
evtInit.screenX = 1506;
evtInit.screenY = 388;
evtInit.clientX = 1326;
evtInit.clientY = 273;
evtInit.buttons = 1;
var element = document.getElementById("element_id");
evt = new MouseEvent(eventType, evtInit);
我以为我必须构建对象!哦,好吧。
现在,如果我可以弄清楚如何设置mozInputSource属性......(单独的问题)。