javascript - 通过jquery或javascript获取图片的坐标

标签 javascript jquery html

我在 div 中添加图像。我想在用户右键单击该图像的位置获取图像的坐标(而不是屏幕的坐标)。我该怎么做?

<div id="Boma" style="width: 1000px; height: 600px; position: relative">
  <img src="Boma_1_2/F16_20170316141116392_0001.jpg" alt="Boma" style="width: 1000px; height: 600px;">  
</div>

最佳答案

我建议您查看 .offset() jQuery 函数:

The .offset() method allows us to retrieve the current position of an element (specifically its border box, which excludes margins) relative to the document.

Here是关于在 Javascript 中使用右键单击的非常清楚且有用的评论。

现在您应该拥有所有工具来创建您想要做的事情。希望对您有所帮助。

关于javascript - 通过jquery或javascript获取图片的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45818046/

相关文章:

html - 通过关注该子元素来设置 contenteditable block 的内部元素的样式

html - 在小屏幕上更改 div 的布局

javascript - jquery UI对话框的返回值

javascript将变量添加到输入字段rails

javascript - 修复了带有 jQ​​uery 可拖动的鼠标指针

用于选择评级的 jQuery 脚本

jQuery UI 对话框调整大小助手

javascript - 在子焦点上更改父 css(不破坏父 :hover css rules)

javascript - 附加左键或右键单击的事件监听器 - onclick 不适用于右键单击

jquery - Div 未在 FireFox 中正确定位