拦截所有鼠标事件 [英] Intercept all mouse events
问题描述
我正在寻找一种拦截网页上所有鼠标事件(尤其是点击次数)的方法。
I'm looking for a way to intercept all mouse events (especially clicks) on a web page.
我的第一直觉是 $ ('body')。click(....)
但如果附加任何其他处理程序,则不捕获该事件事件。
But that's not capturing the event if any other Handler is attached for a particular event.
将问题放在上下文中:我正在为我的网络应用程序编写一个统计模块。我们希望跟踪页面上的所有用户交互以进行调整。如果我们看到他们没有使用功能,我们将添加一个工具提示。
To put the question in context: I'm writing a "statistics" module for my web app. We want to keep track of all user interactions on the pages to adapt. If we see they don't use a functionality we will add a tooltip for example.
我有以下框架:
jQuery,Knockout和一个小框架自制
jQuery, Knockout and a little Framework "home made"
我想要的是一个用鼠标位置每次点击调用的函数。
What I want is a function which is called on each click with mouse position.
推荐答案
尝试这样的事情(没有jQuery):
Try something like this (without jQuery):
var eventCount = 0;
var eventProperty = [];
var TrackMouse = function (mouseEvent) {
eventProperty[eventCount++] = {
id: mouseEvent.toElement.id,
type: 'mouse',
ts: Date.now(),
x: mouseEvent.x,
y: mouseEvent.y
};
console.log("Element id: " + eventProperty[eventCount - 1].id + ", X: " + mouseEvent.x + ", Y: " + mouseEvent.y + "\n");
};
document.addEventListener('click', TrackMouse);
这篇关于拦截所有鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!