[英]CSS :not():hover selector
<div class="parent">
<div class="child">child1</div>
<div class="child">child2</div>
<div class="child">child3</div>
<div class="child">child4</div>
<div class="special">specialChild</div>
</div>
When hovering over one of the .child
elements, I want to give all of the child elements of .parent
to get another background color. 当鼠标悬停在一个.child
元素,我想给所有的子元素的.parent
得到另一个背景颜色。 This should not happen when hovering over the .special
element. 将.special
悬停在.special
元素上时不会发生这种情况。
What I tried so far: 到目前为止我尝试了什么:
.child {
background-color: rgb(255,0,0);
}
.parent:not(.special):hover .child {
background-color: rgb(0, 0, 255);
}
But when hovering over the .special
element, the background color does change (ignoring the not()
selector) 但是当鼠标悬停在.special
元素上时,背景颜色会发生变化(忽略not()
选择器)
What am I missing? 我错过了什么?
EDIT: jQuery may be used as a solution. 编辑:jQuery可以用作解决方案。
To create a list of items it is better to use the appropriate tags. 要创建项目列表,最好使用适当的标记。
Use the
<ul>
tag together with the<li>
tag to create unordered lists . 将<ul>
标记与<li>
标记一起使用以创建无序列表 。 — w3schools.com . - w3schools.com 。
<ul class="parent">
<li class="child">child1</li>
<li class="child">child2</li>
<li class="child">child3</li>
<li class="child">child4</li>
<li class="special">specialChild</li>
</ul>
Then if you want to put the hover only on the items with the class .child
you need to specify the class of element that is NOT the specified element for hover. 然后,如果要将悬停仅放在具有类.child
的项目上,则需要指定不是悬停指定元素的元素类。
ul.parent li:hover:not(.special) {
background-color: rgb(0, 0, 255);
}
You can not use .parent:not(.special)
because in this way you are selecting all ul.parents
except ul.special
(but there is no a parent with that class). 不能使用.parent:not(.special)
因为这样你是选择所有ul.parents
除了ul.special
(但没有与该类父)。 The correct way would be: .parent li:not(.special)
. 正确的方法是: .parent li:not(.special)
。
Wrap the child
elements into an additional div
and apply to it a css rule, eg: 将child
元素包装到另一个div
并将css规则应用于它,例如:
<div class="parent">
<div class="children">
<div class="child">child1</div>
<div class="child">child2</div>
<div class="child">child3</div>
<div class="child">child4</div>
</div>
<div class="special">specialChild</div>
</div>
.children:hover .child {
background: grey;
}
You are using the not
selector on the parent element, which obviously will never have the class special
, the correct way would be: 你正在使用父元素上的not
选择器,它显然永远不会有special
的类,正确的方法是:
.parent:hover > div:not(.special) {
background: papayawhip;
}
But because you are using classes, wouldn't be simpler just .parent:hover .child { ... }
? 但是因为你正在使用类,所以不会更简单.parent:hover .child { ... }
?
EDIT: maybe I misunderstood the question, probably could be asked better. 编辑:也许我误解了这个问题,可能会被问得更好。 However, is something like this you are trying to achieve? 但是,你想要实现这样的事情吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.