在 JavaScript 中确定鼠标指针位于哪个元素之上 [英] Determine which element the mouse pointer is on top of in JavaScript
本文介绍了在 JavaScript 中确定鼠标指针位于哪个元素之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个函数来告诉我鼠标光标在哪个元素上.
I want a function that tells me which element the mouse cursor is over.
因此,例如,如果用户的鼠标悬停在此文本区域上(id wmd-input
),则调用 window.which_element_is_the_mouse_on()
在功能上等同于 <代码>$("#wmd-input").
So, for example, if the user's mouse is over this textarea (with id wmd-input
), calling window.which_element_is_the_mouse_on()
will be functionally equivalent to $("#wmd-input")
.
推荐答案
DEMO
有一个非常酷的函数,叫做 document.elementFromPoint
,它的作用就像它听起来的那样.
DEMO
There's a really cool function called document.elementFromPoint
which does what it sounds like.
我们需要的是找到鼠标的 x 和 y 坐标,然后使用这些值调用它:
What we need is to find the x and y coords of the mouse and then call it using those values:
var x = event.clientX, y = event.clientY,
elementMouseIsOver = document.elementFromPoint(x, y);
这篇关于在 JavaScript 中确定鼠标指针位于哪个元素之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文