event.originalEvent 的 JavaScript 等价物?

标签 javascript jquery events

有人可以建议我与 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/

相关文章:

javascript - 网页宽度超过100% 求助查找问题

javascript - 为什么我的文本框的 onchange 事件会在模糊时触发 - 不是每个键入的字符都会触发?

JavaScript localStorage 返回 1?

javascript - 使用 jquery 删除 Cookie 不起作用

javascript - 插入多对多表 - Node JS MySQL

javascript - 使用变量名调用 jQuery 函数

javascript - 为其他页面预加载外部文件(CSS、JavaScript)

javascript - subview 覆盖父 View 的边界。 [ react native ]

支持全局事件的 Javascript ajax 库

jQuery 事件在 iframe 中不起作用?