JavaScript 是否提供先前发生的事件的历史记录?
我想知道 JavaScript 是否会记录有关先前发生的事件的信息,以便我可以使用这些信息在当前事件中执行特定的操作过程?
例如,我有一个具有 OnBlur
事件处理程序的元素,但我想知道在触发 OnBlur 的
事件发生,例如用户按下 Tab 键、箭头键或单击鼠标将焦点移离元素。OnBlur
事件之前发生的事件
那么 JavaScript 是否提供了任何机制来方便访问事件历史记录,或者人们可以向我提出实现此目的的好方法的想法吗?
I was wondering if JavaScript records information about the prior events that occurred so that I could use the information to perform a specific course of action in the current event?
For example, I have an element that has an OnBlur
event handler, but I would like to know the event that occurred prior to the OnBlur
event that triggered the OnBlur
event to occur, such as the user pressing the Tab key, arrow key, or mouse click that moved focus away from the element.
So does JavaScript provide any mechanism to facilitate access to the events history or can people throw ideas to me at good ways of implementing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JS 中没有任何原生功能可以做到这一点。
您可以只监听您感兴趣的事件并将它们附加到数组或任何对您有用的数据结构中。然后根据需要引用您自己的事件数据库。
所以类似(JQuery 假设..)
There is nothing native in JS that does this..
You could just listen for the events you are interested in and append them to an array or whatever datastructure is useful to you. Then reference your own event database as needed.
So something like (JQuery assumed..)
不,任何浏览器实现都没有维护这样的历史记录:我相信历史 API 维护 URl 遍历,但没有任何内容跟踪单个事件。如果您需要一个,您可能需要自己实现一个。
No, there is no such history maintained by any browser implementation: the history api I believe maintains URl traversal, but nothing tracks individual events. If you need one you will probably need to implement one yourself.
您可以绑定到对象上的任何事件,并在对象上维护该事件以供下一个事件处理程序使用。这是一个 jsfiddle,它显示了对许多事件的绑定并将最后一个事件类型存储在对象上。还有一个点击事件处理程序,它根据对象上保存的最后一个事件做出决定: http://jsfiddle.net /xEjGA/
You could bind to any event on the object and maintain that event on the object for the next event handler. Here is a jsfiddle that shows a binding to many events and storing the last event type on the object. There is also a click event handler that makes a decision based on the last event saved on the object: http://jsfiddle.net/xEjGA/