向鼠标所在的元素添加类
我需要一个函数,将一个类添加到鼠标现在所在的元素。当鼠标移动时,这必须改变,每个元素都应该具有额外的类。
我知道如何使用它来获取元素的 x 和 y
var mouseX = 0;
var mouseY = 0;
$().mousemove(function (e) {
mouseX = e.pageX;
mouseY = e.pageY;
});
,但是如何获取孔元素以添加类?
这里有人可以帮我解决这个问题吗?
I need a function that will add a class to a element that the mouse is on now. this has to change when the mouse moves, every were it goes that element should have the extra class.
I know how to get the x and y of a element using this
var mouseX = 0;
var mouseY = 0;
$().mousemove(function (e) {
mouseX = e.pageX;
mouseY = e.pageY;
});
but then how do i get the hole element in order to add the class?
Can any one here help me with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
那么鼠标悬停的任何元素都会获得该类?
so any element the mouse hovers will get that class?
你可以使用 -
即使你也可以使用这个代码 - jquery 有hust 以如下方式分割
hover
-you can use-
even you can use this code also- jquery has hust splitted
hover
in fallowing way-要了解触发事件的元素,您可以使用事件的“target”属性,然后使用 offset( )
编辑使用“*”选择所有元素或将其附加到文档 $(document)
PS 准备好处理很多事件,我不知道这会如何影响性能。
To know the element that triggered the event you can use the "target" property of the event and then get it's position using offset()
EDIT use '*' to select all elements or attach it to document $(document)
P.S. be prepared to handle A LOT of events, i don't know how this could impact on performance.
这就是我一直在寻找的:
This is what i was looking for: