简体   繁体   English

将鼠标悬停在另一类上时如何更改它的CSS?

[英]How to change css of one class when hovering over another?

I am new to css especially combinators but I am trying to change css of ap element when hovering over a h1 element both of which are in a div of class "a" but I can't figure out how to do it.Here is my code 我是CSS的新手,尤其是组合器,但是我试图将ap元素的css悬停在h1元素上时,这两个元素都在“ a”类的div中,但是我不知道该怎么做。这是我的码

HTML HTML

<div class="a">
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</div>

CSS CSS

div.a h1:hover ~ div.a p {color:red;}

So what am I doing wrong? 那我在做什么错?

You don't need to target the div element again. 您无需再次定位div元素。

All Paragraphs JsFiddle Demo 所有段落 JsFiddle演示

First Paragraph Jsfiddle Demo 第一段 Jsfiddle演示

 div.a h1:hover~p{color:red;} 
 <div class="a"> <h1>This is a Heading</h1> <p>This is a paragraph.</p> <p>This is a paragraph.</p> </div> <p>Some other text.</p> 

If you only want to target the first <p> paragraph tag then you can use 如果只想定位第一个<p>段落标记,则可以使用

 div.a h1:hover+p{color:red;} 
 <div class="a"> <h1>This is a Heading</h1> <p>This is a paragraph.</p> <p>This is a paragraph.</p> </div> 

If you have any questions about the source code above please leave a comment below and I will get back to you as soon as possible. 如果您对上面的源代码有任何疑问,请在下面发表评论,我们将尽快与您联系。

I hope this help. 希望对您有所帮助。 Happy coding! 编码愉快!

This solution should work : 此解决方案应该起作用:

div.a > h1:hover + p {
    color:red;
}

> h1:hover to select h1:hover element where the parent is div with class 'a' > h1:hover以选择h1:hover元素,其中父元素是类为'a'的div

+ p to select p element that are placed immediately after h1:hover element + p选择位于h1:hover元素之后的p元素

If you are using jQuery you may use the following code to transform the paragraph. 如果您使用的是jQuery,则可以使用以下代码转换该段落。

$('div.a h1').hover(function() {
        $(this).siblings('p').css(color: red;);
    }, function() {
        $(this).siblings('p').css( << Previous Css >> );
    }
});

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

相关问题 如何在悬停在另一个不相关类的子类上时更改一个类的css? - How to change css of one class when hovering over child class of another unrelated class? 将鼠标悬停在一个 div 上时,如何将 class 添加到另一个 div? - How do add a class to another div, when hovering over one div? 将鼠标悬停在多个tds上时,如何更改它们的类别? - How do I change the class of several tds when hovering over one of them? 将鼠标悬停在div上时,如何将一个类添加到另一个div? - When hovering over a div, how to add a class to another div? 使用jss悬停时如何更改另一个类的可见性 - How to change visibility of another class when hovering using jss 悬停另一个时更改div的CSS(JQuery) - Change CSS of a div when hovering another (JQuery) 悬停 html 元素时更改 css class - Change css class when hovering html element 如何通过将鼠标悬停在另一个元素上来更改div的类 - How to change class of a div by hovering on another element 如何通过Angular将鼠标悬停在另一个元素上时添加和删除一个元素的类? - How to add and remove class of one element while hovering over another element via Angular? 将鼠标悬停在 HTML 和 CSS 中另一张图像的某个点上时,如何显示 GIF 图像? - How do I display a GIF image when hovering over a point in another image in HTML and CSS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM