0

我习惯于在 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);

虽然这确实调度了点击事件,但 initMouseEvent() 并没有设置 DOM 3 中描述的 MouseEvent 接口的 'buttons' 属性:

[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);

……没有效果。有谁知道该怎么做?

4

2 回答 2

1

使用.createEvent()and initEvent()(= deprecated now ) 你可以使用老式的方式。您可以在对事件构造函数的一次调用中创建和初始化一个事件(每种事件类型都有自己的,例如 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'));

MouseEvent 参考这里

于 2014-10-25T14:45:43.833 回答
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 属性...(单独的问题)。

于 2014-10-27T19:34:46.897 回答