This question already has an answer here:
这个问题在这里已有答案:
- Right click coordinates [duplicate] 1 answer
右键单击坐标[复制] 1个答案
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);
});
#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);
});
#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答案找到鼠标右键单击