简体   繁体   English

将鼠标悬停在子元素上时更改父元素的CSS属性

[英]Change css properties of parent element when hover on child element

I am trying to change properties of parent element when hover on child element. 将鼠标悬停在子元素上时,我试图更改父元素的属性。 Here is my HTML 这是我的HTML

<div class="social-icons">          
    <div class = "innerSocialDiv">
        <a href="#" target="_blank" class="fa fa-facebook fa-lg" title="facebook"></a>
    </div>
</div>

I need to change a CSS property of innerSocialDiv when hovering on fa-facebook . innerSocialDiv悬停在fa-facebook上时,需要更改innerSocialDiv的CSS属性。

Here is what I did in my CSS: 这是我在CSS中所做的:

.fa-facebook:hover  + .innerSocialDiv{
    background-color: black;
}

But it's not working. 但这不起作用。

CSS always work left to right and top to bottom way. CSS始终left to righttop to bottom When you hover a child element then it's parent automatically called hover state. 当您悬停一个子元素时,它的父元素会自动称为悬停状态。
Instead of this you can directly use following CSS 除此之外,您可以直接使用以下CSS

.innerSocialDiv:hover {
    background-color: black;
}

You don't need the + .innerSocialDiv , you can just use: 您不需要+ .innerSocialDiv ,只需使用:

.fa-facebook:hover {
    background-color: black;
}

Try this JQuery 试试这个jQuery

$(".fa.fa-facebook").hover(function(){
    $(".innerSocialDiv").css("background-color", "black");
});

This is working code 这是工作代码

.fa-facebook:hover .innerSocialDiv{
background-color: black;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM