确定鼠标指针在Javascript中的哪个元素 [英] Determine which element the mouse pointer is on top of in Javascript

查看:103
本文介绍了确定鼠标指针在Javascript中的哪个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个告诉我鼠标光标所在元素的功能。

I want a function that tells me which element the mouse cursor is over.

所以,例如,如果用户的鼠标在这个textarea(id = code> 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



有一个非常酷的函数叫做$ code> 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);

document.elementFromPoint

jQuery事件对象

这篇关于确定鼠标指针在Javascript中的哪个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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