鼠标单击事件 - Firefox 上的鼠标坐标
我想找出页面上鼠标单击事件的坐标。写了一小段 JS,它在 Chrome 上运行良好,但在 Firefox 上运行不佳。似乎默认的全局“事件”在 Firefox 中不可用。这是在 Chrome 上运行的代码的较小版本:
$("body").click(function() {
if (event == undefined) // for Chrome, 'event' is not undefined here
var event = window.event;
var xx;
var yy;
if (event) {
// Need this for Chrome (and IE)
xx = event.x;
yy = event.y;
} else {
// firefox
// WHAT SHOULD I DO HERE?
}
console.log('Click called on body.' + xx + ':' + yy);
}
我应该如何管理 Firefox 案例?
I wanted to find out the co-ordinates of the mouse click event on the page. Wrote a little piece of JS which works well on Chrome but not on Firefox. Seems the default global 'event' is not available in Firefox. Here is a smaller version of the code that worked on Chrome:
$("body").click(function() {
if (event == undefined) // for Chrome, 'event' is not undefined here
var event = window.event;
var xx;
var yy;
if (event) {
// Need this for Chrome (and IE)
xx = event.x;
yy = event.y;
} else {
// firefox
// WHAT SHOULD I DO HERE?
}
console.log('Click called on body.' + xx + ':' + yy);
}
What should I manage the Firefox case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
jQuery(看起来您正在使用它)会为您解决事件参数与全局
event
属性问题,因此您无需担心这一点。它还规范pageX
和pageY
属性。jQuery (which it looks like you are using) sorts out the event parameter versus global
event
property issue for you, so you don't need to worry about that. It also normalizespageX
andpageY
properties.JQuery 将事件作为第一个参数传递给您的点击处理程序。
JQuery passes the event as the first argument to your click handler.
您忘记将事件作为参数传递。使用以下格式,并执行 console.log(e) 来获取与其关联的所有数据。但我不确定它有 x 和 y 坐标,您可能必须获取它单击的元素的坐标。
编辑:似乎提供了 pageX 和 pageY 以及其他一些......
You've forgotten to pass the event as a parameter. Use the below format, and do a console.log(e) to get all the data associated with it. But I'm not sure it has x and y coordinates, you might have to get the coordinates of the element it clicked.
EDIT: pageX and pageY seem to be provided, along with a bunch of others...
您需要在单击函数调用中传递事件变量,例如
You need to pass the event variable in the click function call e.g.