有人可以建议我与 JQuery 的 event.originalEvent 属性等效的 Javascript 跨浏览器(应支持 IE 11 和新版本的 Safari、Firefox、Chrome)吗?我尝试在 JS 代码中直接使用 event.originalEvent,它在 Chrome 浏览器上运行得很好,但在 Firefox 上却不行。
我需要此属性来区分元素上的实际鼠标单击和编程单击,但我没有使用 JQuery 的自由。
编辑:答案 here似乎正在做这件事。感谢大家的帮助。
最佳答案
这只是 native 事件
。检查其 isTrusted
属性:
The isTrusted read-only property of the Event interface is a boolean that is true when the event was generated by a user action, and false when the event was created or modified by a script or dispatched via dispatchEvent.
const div = document.querySelector('div');
div.addEventListener('click', (e) => {
if (e.isTrusted) console.log('manual click');
else console.log('script click');
});
setInterval(div.click.bind(div), 2000);
<div>click</div>
关于event.originalEvent 的 JavaScript 等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50014470/