我想知道是否有可能改变它的元素被徘徊时的孩子的风格,而其CSS也被称为当其父母被徘徊?jQuery:风格悬停在父母()和孩子()
为了清楚我的问题,我写下了以下基本代码:
//styling child while mouse is inside child element
$('child').on('hover', function(e){
if (e.type == 'mouseenter'){
$(this).css('background','yellow');
}
})
// styling child while mouse is inside parent
$('child').parents().on('hover', function (e){
if (e.type == 'mouseenter'){
$(this).css('background', 'blue');
}
})
所以,基本上,一旦用户进入父的空间,使孩子的BG蓝色,一旦进入孩子的空间,从改变蓝色到黄色bg。
我看到我为什么不使用CSS的评论,所以这是一个小的解释:我无法选择父元素。我只能从使用父母的孩子那里做()。只要css不支持父母()方法,但我去了jQuery。 :)
为什么不直接用CSS做呢? –
@ibrahimmahrir在我的情况下,我没有权力只选择它自己的父元素。我只能从孩子选择它,使用父母()。和CSS不支持这一点呢。 –
@RachelNicolas如果你不能直接选择元素,那么当然你可以根据它们在DOM中的位置来选择它们,比如'#container .panel> div'。 CSS绝对是这里的答案。同样使用'hover()'然后检查'mouseenter'事件有点多余 - 只需要使用''('mouseenter',fn)' –