简体   繁体   English

相邻的兄弟选择器CSS

[英]Adjacent sibling selector CSS

I have 2 elements inside my Div,both have same class name ,I want to hide my first element with the class name .cart .I am using the below code. 我的Div中有2个元素,两个都有相同的类名,我想用类名.cart隐藏我的第一个元素。我正在使用下面的代码。

.component-bottom  .component-basket + .cart{
     display:none;
}

I have added my html structure: 我添加了我的html结构:

<div class="component-bottom">
  <div class="component-basket">
    <div class="cart">
    </div>
    <div class="cart">
    </div>
  </div>
</div>

Am i using correct code? 我使用正确的代码吗?

Please advice me.. 请指教我..

You can use a direct child selector for the .cart element: 您可以为.cart元素使用直接子选择器:

.component-bottom .component-basket > .cart
{
     display:none;
}


Now you only want the first element of this selector. 现在你只想要这个选择器的第一个元素。 There isn't an original selector for this, but you can make a overwrite selector for this. 没有原始选择器,但您可以为此创建一个覆盖选择器。
You can overwrite all but the first one ElementA ~ ElementB : 您可以覆盖除第一个 ElementA ~ ElementB

.component-bottom .component-basket > .cart ~ .cart
{
    display:block;
}

This search for all .cart elements inside .component-basket where ANY previous adjacent sibling is .cart . 这将搜索.cart .component-basket中的所有.cart元素,其中任何前一个相邻的兄弟是.cart The first of the element doesn't have a previous sibling of this class, so it would not be selected. 元素的第一个没有此类的前一个兄弟,因此不会被选中。

This is called a general sibling selector . 这称为通用兄弟选择器

jsFiddle 的jsfiddle

This should support IE7 and above: 这应该支持IE7及以上版本:

Note Requires Windows Internet Explorer 7 or later. 注意需要Windows Internet Explorer 7或更高版本。

source: http://msdn.microsoft.com/en-us/library/ie/aa358824(v=vs.85).aspx 来源: http//msdn.microsoft.com/en-us/library/ie/aa358824(v = vs。85).aspx


an easier solution commented by @jrConway: @jrConway评论的更简单的解决方案:

Make it display: block by default and use: 使其显示:默认阻止并使用:

.component-bottom .component-basket > .cart:first-child
{
     display: none;
}

Example

Note that this only work when you use ONLY .cart as child element. 请注意,这仅在您使用.cart作为子元素时才有效。 Whenever an other class is at the first 'place' it will not work. 每当另一个班级位于第一个“地方”时,它将无法工作。

Using adjacent sibling selector won't work here, as your element is nested inside .component-basket and hence it fails.. Simple way is to call a class on the element you want to hide, if you cannot change the DOM than you can use first-child or nth-of-type(1) 使用相邻的兄弟选择器在这里不起作用,因为你的元素嵌套在.component-basket ,因此它失败了。简单的方法是在你要隐藏的元素上调用一个class ,如果你不能改变DOM那么你可以使用first-childnth-of-type(1) first-child nth-of-type(1)

.component-bottom .component-basket div.cart:nth-of-type(1) {
     display:none;
}

Demo 演示


As @Vucko already commented, nth-of-type() is a CSS3 spec pseudo.. Hence if you want to support legacy browsers, you can use Selectivizr , this will save you a lot of classes/ids. 正如@Vucko已经评论过的那样, nth-of-type()是一个CSS3规范伪。因此,如果你想支持旧浏览器,你可以使用Selectivizr ,这将为你节省很多类/ ID。

As understood, check this might help 据了解,检查这可能会有所帮助

CSS CSS

.cart{
    display:none;
}

.component-bottom  .component-basket
{
    //some common properties
}

HTML HTML

<div class="component-bottom">
    <div class="component-basket cart">component-basket Hidden div</div>
    <div class="component-basket">component-basket visible div</div>
</div>

This will hide the div with the cart class (the First div) 这将使用购物车类隐藏div(第一个div)

Thanks, 谢谢,

Dhiraj DHIRAJ

Stick this in your CSS file: 将其粘贴在CSS文件中:

.hide {
    display: none;
}

Then add that class to whatever element you want hidden like so: 然后将该类添加到您想隐藏的任何元素,如下所示:

<div class="component-bottom">
    <div class="component-basket">Foo</div>
    <div class="component-basket cart hide">Foo</div>
</div>

The advantage of this method is that you get to re-use that "hide" class anywhere you want. 这种方法的优点是你可以在任何你想要的地方重用“隐藏”类。

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

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