Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery get元素中光标所在的位置_Jquery_Jquery Plugins - Fatal编程技术网

jquery get元素中光标所在的位置

jquery get元素中光标所在的位置,jquery,jquery-plugins,Jquery,Jquery Plugins,我正在用javascript和jquery为我的网站构建一个测试工具。现在,我想在光标经过预览框中的元素时,为每个元素显示一个小的鼠标悬停菜单。有可能这样做吗?我做过类似的事情 $('body').hover(function(event){ console.log(event.target.nodeName); // to see if it's showing up the element }); 但它只触发一次。由于我不想使用click,因为我还想显示锚元素的菜单,

我正在用javascript和jquery为我的网站构建一个测试工具。现在,我想在光标经过预览框中的元素时,为每个元素显示一个小的鼠标悬停菜单。有可能这样做吗?我做过类似的事情

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

但它只触发一次。由于我不想使用click,因为我还想显示锚元素的菜单,所以我有点迷路了

您可以使用
document.element frompoint

var element = document.elementFromPoint(x, y);
例如:

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

文档:

我相信您希望在此处使用
mousemove
事件,而不是
悬停
事件

$('body').mousemove(function(evt){
    console.log(evt.target);
});
请记住使用
mousemove
时要格外小心


尝试下面的编码,它将帮助您

    <body onMouseMove="javaScript:mouseEventHandler(event);">

    <script>
    function mouseEventHandler(mEvent) {
// Internet Explorer
                alert(mEvent.srcElement.nodeName); //Display Node Name
                alert(mEvent.srcElement.id); //Display Element ID
//FireFox
                alert(mEvent.target.nodeName);//Display Node Name
                alert(mEvent.target.id);//Display Element ID
            }
    </script>

功能mouseEventHandler(mEvent){
//Internet Explorer
警报(mEvent.srceelement.nodeName);//显示节点名称
警报(mEvent.srcielement.id);//显示元素id
//火狐
警报(mEvent.target.nodeName);//显示节点名称
警报(mEvent.target.id);//显示元素id
}

如果您使用的是键盘而不是鼠标: 不是jQuery,只是针对感兴趣的人的普通JavaScript:

getSelection().getRangeAt(0).commonAncestorContainer.parentNode

有三种方法可以做到这一点:

  • 诸如此类:

    $('body').on('mousemove', function() {
     console.log($(':hover').last().attr('name'));
    });
    
  • 出于调试目的,您可以在chrome控制台中键入jush
    $(':hover')。last()
    然后将鼠标悬停在所需位置,然后按Enter键运行此控制台命令

  • 如果您想稳定地使用它,我建议不要使用mousemove事件。用那样的东西

    $('.one_of_your_trigger_element').on('mouseenter', function() {
     var hover_element = $(':hover').last();
     ...
    });
    

  • 这将继续触发一次。还有,当您可以从
    事件
    免费获取元素时,为什么会有人想使用
    document.elementFromPoint
    ?@Alexander-在这种情况下,是的。但对于OP最初的问题“jquery get元素,光标在哪里”-
    document.elementFromPoint
    才是解决之道。