使用jQuery如何在目标元素上获得右键单击坐标[重复]

时间:2022-10-27 12:38:16

This question already has an answer here:

这个问题在这里已有答案:

Is there any way to find the co-ordinates of a right click event on a div element as I have to set context menu based on the position of click.

有没有办法在div元素上找到右键单击事件的坐标,因为我必须根据点击的位置设置上下文菜单。

Any help and suggestion will be appreciated. Thanks.

任何帮助和建议将不胜感激。谢谢。

2 个解决方案

#1


1  

You can try:

你可以试试:

$('div').on('contextmenu', function (e) {
    console.log(e.pageX);
    console.log(e.pageY);
});

And for whole page:

整页:

$('div').on('contextmenu', function (e) {
    console.log(e.clientX);
    console.log(e.clientY);
});

Fiddle: https://jsfiddle.net/shree/awf5u2xx/1/

#2


1  

$(document).ready(function(){ 
  document.oncontextmenu = function() {return false;};

  $(document).on('mousedown', '#TargetElementId', function (e){ 
    if( e.button == 2 ) { // Right mouse button clicked

      return {e.pageX, e.pageY} //return co-ordinates
    } 
    return true; 
  }); 
});

From SO Answer to find right mouse button clicked

从SO答案找到鼠标右键单击

#1


1  

You can try:

你可以试试:

$('div').on('contextmenu', function (e) {
    console.log(e.pageX);
    console.log(e.pageY);
});

And for whole page:

整页:

$('div').on('contextmenu', function (e) {
    console.log(e.clientX);
    console.log(e.clientY);
});

Fiddle: https://jsfiddle.net/shree/awf5u2xx/1/

#2


1  

$(document).ready(function(){ 
  document.oncontextmenu = function() {return false;};

  $(document).on('mousedown', '#TargetElementId', function (e){ 
    if( e.button == 2 ) { // Right mouse button clicked

      return {e.pageX, e.pageY} //return co-ordinates
    } 
    return true; 
  }); 
});

From SO Answer to find right mouse button clicked

从SO答案找到鼠标右键单击