简体   繁体   English

锚悬停和相邻的同级

[英]Anchor hover and adjacent sibling

Imagine this html structure: 想象一下这个html结构:

<p><a></a></p>
<div><a><img></a></div>

I want to hide all images inside a div after a p tag , to do that, I simple use this code: 我想在p tag后隐藏div所有图像,为此,我只需使用以下代码:

p + div {display:none;}

but when I try to show those images by hover the anchor inside the p tag before, it doesn't work the same way by using this: 但是当我尝试显示通过悬停这些图像anchor内部p tag之前,它不会使用此相同的方式工作:

p > a:hover + div {display:block;}

if I use only p instead: 如果我只使用p代替:

p:hover + div {display:block;}

it works, but it's not what I pretend. 它有效,但这不是我假装的。

Since a it's a child of p tag , adjacent sibling "+" doesn't work? 由于ap tag的子p tag ,因此相邻的兄弟“ +”不起作用吗?

Since a it's a child of p tag, adjacent sibling "+" doesn't work? 由于a是p标签的子元素,因此相邻的同级“ +”不起作用吗?

Correct. 正确。 The div is a sibling of the p , not the a . div是的兄弟p ,而不是a

You could set pointer-events for the p to "none", and pointer-events for the a to "auto". 您可以将p pointer-events设置为“ none”,将a pointer-events为“ auto”。

You could then use your working p:hover + div code, but it would act like it's working for the anchor only: 然后,您可以使用工作中的p:hover + div代码,但它的行为就像仅对锚点起作用:

 p + div { display:none; } p { pointer-events: none; } a { pointer-events: auto; } p:hover + div { display: block; } 
 <p> <a href="#">This is an anchor.</a> <br> Lorem ipsum et cetera. </p> <div> Cool picture: <a> <img src="http://lorempixel.com/50/50"> </a> </div> 

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

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