[英]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.