如何检查鼠标单击事件是模拟的还是本地的? (非jQuery方式) [英] How to check if mouse click event is simulated or is native? (non-jQuery way)
问题描述
使用jQuery很容易。使用纯JavaScript并非如此,至少,要找到如何做到这一点并不容易。 if(event.clientX&& event.clientY){...}
进行某种检测,但不是100%准确。
With jQuery it's easy. With pure JavaScript isn't, at least it isn't easy to find out how. if (event.clientX && event.clientY) { ... }
gives kind-of detection, but it's not 100% accurate.
推荐答案
有是的一种方法,但是IMHO颇具争议。尝试以下操作:
There is a way, but it's quite controversial IMHO. Try this:
HTMLElement.prototype.dispatchEvent = (function(dispev) {
return function(event) {
// Do your stuff
dispev.call(this, event);
};
})(HTMLElement.prototype.dispatchEvent);
借助此功能,您可以捕获对 dispatchEvent的每次调用
调度以编程方式生成的事件时。当然,这会放慢调度的速度,但是除非您要尝试触发许多事件,否则这不是问题。
With this, you're able to catch every call to dispatchEvent
when a programmatically generated event is dispatched. Of course, this would slow down a bit the dispatching, but shouldn't be a problem unless you're trying to fire a lot of events.
在IE8中,您必须替换 Element.prototype.fireEvent
,而在IE7及更低版本中,则无法执行任何操作。
In IE8 you must replace Element.prototype.fireEvent
instead, while in IE7 and lower it's just not possible to do anything about it.
这篇关于如何检查鼠标单击事件是模拟的还是本地的? (非jQuery方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!