如何从正文上的onClick事件中获取鼠标单击的绝对位置? [英] How do I get the absolute position of a mouse click from an onClick event on the body?

查看:385
本文介绍了如何从正文上的onClick事件中获取鼠标单击的绝对位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得相对于浏览器/正文的鼠标点击的绝对位置(顶部和左侧),正文中的任何父元素。

I am trying to get the absolute position (top and left) of a mouse click relative to the browser/body, not any parent elements within the body.

我有一个绑定到正文的监听器,但是e.pageX和e.pageY给了我相对于div的位置。

I have a listener bound to the body, but e.pageX and e.pageY are giving me the position relative to a div.

注意我可以利用jQuery和YUI函数。

Note that I can leverage jQuery and YUI functions.

目前无效的代码:

//getting the position
function _handleClick(e) {
    var data = { absX: e.pageX, absY: e.pageY};
    _logClickData(data);
}

//binding the function
var methods = {
    init: function () {
        $("body").click(_handleClick);
    }
};


推荐答案

根据这个( http://docs.jquery.com/Tutorials:Mouse_Position ),那些应该给你绝对的位置。 offsetX / Y 为您提供相对位置。

According to this (http://docs.jquery.com/Tutorials:Mouse_Position), those should give you absolute positions. offsetX/Y gives you the relative position.

2013年11月编辑:原文鼠标位置链接似乎已被破坏,但 pageX的文档 包含一个使用jQuery pageX / Y 的示例。 有关偏移量方法的页面也包含相关内容例子。

Edit November 2013: the original "Mouse Position" link seems to be broken, but the documentation for pageX contains an example that utilizes jQuery pageX/Y. The page about the offset method also contains relevant examples.

这篇关于如何从正文上的onClick事件中获取鼠标单击的绝对位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆