jquery单击当前光标位置

时间:2022-11-21 15:37:01

I know that with jquery I can select an element and invoke a click. However, what I am looking to do is simply invoke a click on where ever the current cursor is.

我知道使用jquery我可以选择一个元素并调用一个单击。但是,我要做的只是调用当前光标所在的位置。

For example something like this:

例如这样的事情:

jQuery().click();

1 个解决方案

#1


0  

I think this might help:

我认为这可能会有所帮助:

How to simulate a click by using x,y coordinates in JavaScript?

如何使用JavaScript中的x,y坐标模拟单击?

maybe something like:

也许是这样的:

$(document).click(function(e) {
   var x = e.clientX;
   var y = e.clientY;
   document.elementFromPoint(x, y).click();
})

Getting the user's mouse position can only be done via an event. Let's say your mouse is broken and it will track, but won't click, you could trigger a 'fake' click at the current position through other means:

获取用户的鼠标位置只能通过事件来完成。假设您的鼠标已损坏且会跟踪,但不会点击,您可以通过其他方式触发当前位置的“假”点击:

var x, y;

$(document).mousemove(function(e) {
    x = e.clientX;
    y = e.clientY;
})

$(document).keyup(function(e) {
    // if the key pressed was return, click at the current cursor position
    if (e.keyCode === 13) {
        document.elementFromPoint(x, y).click();
    }
})

$(document).keyup(function(e) {
    // if the key pressed was space, click 50px below the current cursor position
    if (e.keyCode === 32) {
        document.elementFromPoint(x, y + 50).click();
    }
})

tested and working, now you can click with your keyboard!

测试和工作,现在你可以用键盘点击!

Note that document.elementFromPoint(x, y) looks for elements at the coordinates relative to the viewport. So you'll be using e.clientX & e.clientY, not e.pageX & e.pageY.

请注意,document.elementFromPoint(x,y)在相对于视口的坐标处查找元素。所以你将使用e.clientX和e.clientY,而不是e.pageX和e.pageY。

#1


0  

I think this might help:

我认为这可能会有所帮助:

How to simulate a click by using x,y coordinates in JavaScript?

如何使用JavaScript中的x,y坐标模拟单击?

maybe something like:

也许是这样的:

$(document).click(function(e) {
   var x = e.clientX;
   var y = e.clientY;
   document.elementFromPoint(x, y).click();
})

Getting the user's mouse position can only be done via an event. Let's say your mouse is broken and it will track, but won't click, you could trigger a 'fake' click at the current position through other means:

获取用户的鼠标位置只能通过事件来完成。假设您的鼠标已损坏且会跟踪,但不会点击,您可以通过其他方式触发当前位置的“假”点击:

var x, y;

$(document).mousemove(function(e) {
    x = e.clientX;
    y = e.clientY;
})

$(document).keyup(function(e) {
    // if the key pressed was return, click at the current cursor position
    if (e.keyCode === 13) {
        document.elementFromPoint(x, y).click();
    }
})

$(document).keyup(function(e) {
    // if the key pressed was space, click 50px below the current cursor position
    if (e.keyCode === 32) {
        document.elementFromPoint(x, y + 50).click();
    }
})

tested and working, now you can click with your keyboard!

测试和工作,现在你可以用键盘点击!

Note that document.elementFromPoint(x, y) looks for elements at the coordinates relative to the viewport. So you'll be using e.clientX & e.clientY, not e.pageX & e.pageY.

请注意,document.elementFromPoint(x,y)在相对于视口的坐标处查找元素。所以你将使用e.clientX和e.clientY,而不是e.pageX和e.pageY。