jquery获取光标所在的元素

Joh*_*000 15 jquery jquery-plugins

我正在使用javascript和jquery为我的网站构建一个splittesting工具.现在我想显示每个元素,当光标经过我的预览框中的元素时,我想分割一些小的hovermenu.有没有可能做这样的事情?我喜欢这样的事情

$('body').hover(function(event){
    console.log(event.target.nodeName);
    // to see if it's showing up the element   
});
Run Code Online (Sandbox Code Playgroud)

但它只触发一次.因为我不想使用点击,因为我也想在锚元素上显示菜单我有点迷失

Ale*_*der 11

我相信你想在mousemove这里使用这个事件而不是hover事件.

$('body').mousemove(function(evt){
    console.log(evt.target);
});
Run Code Online (Sandbox Code Playgroud)

请记住mousemove谨慎使用.

在这里查看示例.


tec*_*bar 10

你可以用document.elementFromPoint它.

var element = document.elementFromPoint(x, y);
Run Code Online (Sandbox Code Playgroud)

例如:

$('body').hover(function(event){
    var el = document.elementFromPoint(event.pageX, event.pageY);
});
Run Code Online (Sandbox Code Playgroud)

文档:https://developer.mozilla.org/en-US/docs/DOM/document.elementFromPoint

  • @Alexander - 在这个特殊情况下,是的.但是对于OP的原始问题"jquery get cursor where the cursor is" - "document.elementFromPoint"是要走的路. (3认同)

Ste*_*ves 10

如果您使用的是键盘而不是鼠标:不是jQuery,只对那些感兴趣的人使用简单的JavaScript:

getSelection().getRangeAt(0).commonAncestorContainer.parentNode
Run Code Online (Sandbox Code Playgroud)