jQuery - 悬停一个元素来添加一个类?

时间:2013-07-04 08:02:11

标签: jquery css jquery-selectors

$(".test").hover(function(){
  $(this).addClass("move");        
})

上面的 jQuery 需要一些修复。我的 CSS 选择器目前是这样的:

.test .selectme

并将鼠标悬停在.test元素上,结果为:

.test.move .selectme

当我将鼠标悬停在.test元素上时,我试图改变这一点:

.test .selectme.move

所以基本上当我将鼠标悬停在.selectme .move上时,我需要jQuery给parent .test课程。

2 个答案:

答案 0 :(得分:5)

您需要在selectme中找到menu项,然后将类添加到其中

$(".menu").hover(function(){
  $(this).find('.selectme').addClass("move"); //You may use toggleClass('move') if you want to remove the class when the mouse leaves menu item
})

答案 1 :(得分:1)

$(".test").hover(function(){
    $('.selectme',this).addClass("move"); //This is simpler, find the selectme that's inside of .test
})