How to check if mouse click event is simulated or is native? (non-jQuery way)

℡╲_俬逩灬. 提交于 2019-12-06 09:01:21

问题


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.


回答1:


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);

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.

In IE8 you must replace Element.prototype.fireEvent instead, while in IE7 and lower it's just not possible to do anything about it.




回答2:


Event.isTrusted can be used upto a satisfactory level.



来源:https://stackoverflow.com/questions/16178675/how-to-check-if-mouse-click-event-is-simulated-or-is-native-non-jquery-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!