如何从正文上的onClick事件中获取鼠标单击的绝对位置?

编程入门 行业动态 更新时间:2024-10-26 22:26:15
本文介绍了如何从正文上的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); } };

推荐答案

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

According to this (docs.jquery/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事件中获取鼠标单击的绝对位置?

本文发布于:2023-10-08 04:24:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471514.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   单击   位置   正文   事件中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!