简体   繁体   English

如何判断刚刚触发的Javascript事件

[英]How to tell what Javascript event just fired

Is there any way in Javascript to tell what the most recently-fired event was? Javascript中有什么办法可以告诉最近触发的事件是什么? Reason I'm asking is I'm working on a site that isn't behaving properly on one of those new Ultrabooks that's running Windows 8 and is a laptop with a touch screen. 我要问的原因是,我正在一个网站上工作不正常,这些网站运行的是Windows 8且是带有触摸屏的笔记本电脑的那些新Ultrabook。 If you use the standard mouse functionality (with a touchpad or an actual mouse), things work fine, but if you use the touch screen, things don't. 如果您使用标准的鼠标功能(使用触摸板或实际鼠标),则一切正常,但如果使用触摸屏,则效果不佳。

This only happens with IE; 这仅在IE中发生; Chrome has its own issues (which I have fixed in the code), and Firefox hasn't given us any problems. Chrome有其自身的问题(已在代码中修复),而Firefox没有给我们任何问题。

Basically, the functionality we have includes a "hoverIntent" block, and if you use the touch screen on IE, it registers both the "over" and "out" functions, which is a problem. 基本上,我们拥有的功能包括一个“ hoverIntent”块,如果您在IE上使用触摸屏,它会同时注册“ over”和“ out”功能,这是一个问题。

However, if there was a way for me to tell whether the last thing that happened was that the user TOUCHED THE SCREEN or CLICKED WITH A MOUSE, I'd have a solution in place. 但是,如果有办法告诉我最后发生的事情是用户触摸了屏幕还是用鼠标敲了一下,我将采取适当的解决方案。 But I couldn't tell if there's a way to do that. 但是我无法确定是否有办法做到这一点。

The only thing I could find was tacking on ".data('events')" on an element, but what returns is "click" regardless of whether it was an actual mouse click or a tap on the screen. 我唯一能找到的就是在元素上加上“ .data('events')”,但是无论是实际单击鼠标还是在屏幕上点击,返回的都是“单击”。

Is there a way to do this??? 有没有办法做到这一点???

The browser does not have a standard way of recording events that happened previously. 浏览器没有记录以前发生的事件的标准方法。 If you want to know what events happened prior to the current event, then you will have to install an event handler for those events and record them yourself so you can then look back at them at some future time. 如果您想知道当前事件之前发生了什么事件,那么您将必须为这些事件安装一个事件处理程序并自己进行记录,以便以后在将来查看它们。

For events that propagate, you could install some event handlers on the document object and record both event and target for the last N events. 对于传播的事件,您可以在document对象上安装一些事件处理程序,并记录最后N个事件的事件和目标。

If you're just trying to figure out what event the current event is, then you can examine the type property of the event object that is passed into the event handler as in e.type . 如果您只是想弄清楚当前事件是什么事件,那么可以像e.type一样检查传递到事件处理程序中的event对象的type属性。

You can add an event to your function arguments and then use event.type to check which event is triggered. 您可以将事件添加到函数参数中,然后使用event.type检查触发了哪个事件。 ex: 例如:

var x = function(e) { 
    alert(e.type);
}

So I found out that IE has a completely different set of touch events from what EVERY OTHER BROWSER IN THE UNIVERSE has. 因此,我发现IE的触摸事件与Universe中的其他浏览器完全不同。 ughhh. ug。 Instead of "touchstart," you use "MSPointerDown," etc. My solution was basically to write new event handlers for MSIE's touch device events. 您可以使用“ MSPointerDown”代替“ touchstart”,等等。我的解决方案基本上是为MSIE的触摸设备事件编写新的事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM