向鼠标所在的元素添加类

发布于 2024-11-15 07:23:33 字数 273 浏览 3 评论 0原文

我需要一个函数,将一个类添加到鼠标现在所在的元素。当鼠标移动时,这必须改变,每个元素都应该具有额外的类。

我知道如何使用它来获取元素的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

始终不够爱げ你 2024-11-22 07:23:33

那么鼠标悬停的任何元素都会获得该类?

$('*').hover(function(){ $(this).addClass('theClass'); });

so any element the mouse hovers will get that class?

$('*').hover(function(){ $(this).addClass('theClass'); });
聆听风音 2024-11-22 07:23:33

你可以使用 -

  1. mouseover

即使你也可以使用这个代码 - jquery 有hust 以如下方式分割hover -

$(".hoverme").live("mouseover mouseout", function(event) {
  if ( event.type == "mouseover" ) {
    // do something on mouseover
  } else {
    // do something on mouseout
  }
});

you can use-

  1. mouseover

even you can use this code also- jquery has hust splitted hover in fallowing way-

$(".hoverme").live("mouseover mouseout", function(event) {
  if ( event.type == "mouseover" ) {
    // do something on mouseover
  } else {
    // do something on mouseout
  }
});
伪装你 2024-11-22 07:23:33
$('*').hover(function(){
    $(this).addClass('special'); // mouse over
},
function(){
    $(this).removeClass('special'); // mouse out
});
$('*').hover(function(){
    $(this).addClass('special'); // mouse over
},
function(){
    $(this).removeClass('special'); // mouse out
});
一直在等你来 2024-11-22 07:23:33

要了解触发事件的元素,您可以使用事件的“target”属性,然后使用 offset( )

$('*').mousemove(function (e) {
    var target = e.target //this is the element that triggered the evnt
    //do what you want with target: to get it's position:
     var position = $(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()

$('*').mousemove(function (e) {
    var target = e.target //this is the element that triggered the evnt
    //do what you want with target: to get it's position:
     var position = $(target).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.

最冷一天 2024-11-22 07:23:33

这就是我一直在寻找的:

$('*').hover(
    function (event) {
        var elem = document.elementFromPoint(event.clientX, event.clientY);
        $(elem).addClass('khoverElem');
    },
    function (event) {
        $('.khoverElem').removeClass('khoverElem');

    }
);

This is what i was looking for:

$('*').hover(
    function (event) {
        var elem = document.elementFromPoint(event.clientX, event.clientY);
        $(elem).addClass('khoverElem');
    },
    function (event) {
        $('.khoverElem').removeClass('khoverElem');

    }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文