所以我有一个样式为pointer-events: none;
的元素,以防止用户点击它,但是我想点击它作为我的JS代码的一部分,这就是我做的:
function click(x, y) {
element.style.pointerEvents = "auto";
let event = new MouseEvent("mousedown");
event.clientX = x;
event.clientY = y;
element.dispatchEvent(event);
element.style.pointerEvents = "none";
}
但是,这并没有做任何事情,因为element.style.pointerEvents = "auto";
没有立即更改,所以我将后一部分包装成setTimeout
,延迟时间为{ {1}}:
200
显然只有function click(x, y) {
element.style.pointerEvents = "auto";
setTimeout(function() {
let event = new MouseEvent("mousedown");
event.clientX = x;
event.clientY = y;
element.dispatchEvent(event);
element.style.pointerEvents = "none";
}, 200);
}
似乎不够,有没有办法在不使用hacky,丑陋100
的情况下完成所有这些操作?