jQuery:如何获得我徘徊在整个文档主体全局的最内层的dom元素? [英] jQuery: how to get the innermost dom element I'm hovering over, globally for the whole document body?

查看:184
本文介绍了jQuery:如何获得我徘徊在整个文档主体全局的最内层的dom元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测整个文档主体上的鼠标移动,并且能够准确地确定我正在盘旋的DOM的哪一部分。通过哪一部分,我指的是鼠标当前结束的最内层的DOM元素。

I'd like to detect mouse movements over the whole document body, and be able to tell exactly which part of the DOM I'm hovering over. By "Which part" I mean the innermost DOM element that the mouse is currently over.

我可以将悬停绑定到整个文档主体,但是这个$(this )会给我$(body),而不是最内层的元素。或者我可以迭代遍历整个DOM并将悬停绑定到每个元素,但这将是一个严重的矫枉过正IMO。

I could bind a hover to the whole document body, but then the $(this) would give me the $(body), not the innermost element. Or I could iterate over the whole DOM recursively and bind a hover to each elements, but that would be a serious overkill IMO.

有没有简单的方法来实现这一点?

Is there an easy way to achieve this?

推荐答案

基于Jasie的建议,我最终使用了以下代码:

Based on Jasie's suggestion, I ended up using the following code:

var currentNode = null;
$('body').mousemove(function(event) {
  if ($(event.target) !== currentNode) {
    currentNode = $(event.target);
    console.log(currentNode);
  }
});

这篇关于jQuery:如何获得我徘徊在整个文档主体全局的最内层的dom元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆