我习惯于在 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);
……没有效果。有谁知道该怎么做?