鼠标单击事件 - Firefox 上的鼠标坐标

发布于 2024-12-11 10:20:30 字数 569 浏览 0 评论 0原文

我想找出页面上鼠标单击事件的坐标。写了一小段 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

念三年u 2024-12-18 10:20:30

jQuery(看起来您正在使用它)会为您解决事件参数与全局 event 属性问题,因此您无需担心这一点。它还规范 pageXpageY 属性。

$("body").click(function(evt) {
    var xx = evt.pageX;
    var yy = evt.pageY;

    console.log('Click called on body.' + xx + ':' + yy);
});

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 normalizes pageX and pageY properties.

$("body").click(function(evt) {
    var xx = evt.pageX;
    var yy = evt.pageY;

    console.log('Click called on body.' + xx + ':' + yy);
});
拔了角的鹿 2024-12-18 10:20:30

JQuery 将事件作为第一个参数传递给您的点击处理程序。

JQuery passes the event as the first argument to your click handler.

一个人练习一个人 2024-12-18 10:20:30

您忘记将事件作为参数传递。使用以下格式,并执行 console.log(e) 来获取与其关联的所有数据。但我不确定它有 x 和 y 坐标,您可能必须获取它单击的元素的坐标。

$("body").click(function(e) {
}

编辑:似乎提供了 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.

$("body").click(function(e) {
}

EDIT: pageX and pageY seem to be provided, along with a bunch of others...

为人所爱 2024-12-18 10:20:30

您需要在单击函数调用中传递事件变量,例如

.click(function(e){
    console.log(e);
});

You need to pass the event variable in the click function call e.g.

.click(function(e){
    console.log(e);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文