JavaScript:如何检查鼠标单击事件是模拟还是本机? (非jquery方式) [英] JavaScript: 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.
推荐答案
是的一种方式,但它很有争议恕我直言。试试这个:
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.
这篇关于JavaScript:如何检查鼠标单击事件是模拟还是本机? (非jquery方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!