简体   繁体   English

Display:none / Display:block与组合的同级选择器

[英]Display:none / Display:block with Combined Sibling Selectors

I am trying to make the sub-menu with class = "category-nav" only valid for the "color" option (so it should not exist or be displayed for any of the other options). 我正在尝试使class =“ category-nav”的子菜单仅对“ color”选项有效(因此,该菜单不应该存在或对其他任何选项都显示)。

The display: none works but when I try to do display block using combined sibling selectors to specify displaying submenu only for the option "color" it does not work. 显示:没有任何效果,但是当我尝试使用组合的同级选择器指定仅针对“颜色”选项的显示子菜单时,它不起作用。 (.add-nav #color ~ .category-nav {display:block}). (.add-nav #color〜.category-nav {display:block})。 What am I doing wrong? 我究竟做错了什么? Is there another way to do this with css? 还有另一种方法可以使用CSS吗? I would prefer not to use JavaScript for this as the page is already JavaScript heavy. 我不希望为此使用JavaScript,因为该页面已经包含大量JavaScript。

<ul class="add-nav">
                            <li><a href="#">shape</a></li>
                            <li id = "color" class="active"><a href="#">color</a></li> 
                            <li><a href="#">pattern</a></li>
                            <li><a href="#">size</a></li>
                            <li><a href="#">overall</a></li> 
</ul>
                        <ul class="category-nav">
                            <li class="active"><a href="#">all</a></li>
                            <li><a href="#">Red</a></li>
                            <li><a href="#">Orange</a></li>
                            <li><a href="#">Yellow</a></li>
                            <li><a href="#">Green</a></li>
                            <li><a href="#">Blue</a></li>
                            <li><a href="#">Purple</a></li>
                        </ul>

Here's the css 这是CSS

.add-nav{
    width:100%;
    padding:0;
    margin:10px 0 0px;
    list-style:none;
    border-bottom:1px solid #cecece;
    display:table;
    text-transform:uppercase;
    font-size:10px;
    line-height:14px;
}
.add-nav li{
    text-align:center;
    display:table-cell;
    vertical-align:bottom;
}
.add-nav li a{
    display:block;
    padding:21px 5px 16px;
    color:#636363;
}
.add-nav li a:hover,
.add-nav .active a{
    font-weight:700;
    text-decoration:none;
    color:#272727;
    background:#f2f2f2;
}
.category-nav{
    padding:5px 13px 7px;
    margin:0 0 0px;
    list-style:none;
    background:#f2f2f2;
    width:100%;
    font-size:10px;
    font-variant:small-caps;
    display:table;
    box-sizing:border-box;
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
}
.category-nav li{
    text-align:center;
    display:table-cell;
    vertical-align:bottom;
}
.category-nav li a{color:#636363;}
.category-nav li a:hover{
        text-decoration:none;
    color:#ff6699;
}
\\.category-nav{
        display:none;\\
}
.category-nav .active a{
    color:#ff6699;
    font-weight:700;
}

Here is the code in JS Fiddle 这是JS Fiddle中的代码

What you are looking for is a CSS parent selector, which doesn't exist . 您正在寻找的是CSS父选择器,该选择器不存在

Your code says: 您的代码说:

.add-nav li.active #color ~ .category-nav     // I added the .active class

What this actually does is select all siblings of #color that match .category-nav . 这实际上是选择与.category-nav匹配的所有#color兄弟姐妹。 Now, .category-nav is not a sibling of #color ; 现在, .category-nav不是#color的兄弟姐妹; it's a sibling of .add-nav . 这是.add-nav的兄弟姐妹。 What you really want to do is select siblings of .add-nav ... but only when it contains li.active #color . 真正想要做的是选择.add-nav ...的兄弟姐妹,但是仅当它包含 li.active #color In other words, you want to define something, and then select, not the thing itself, but its parent. 换句话说,您想定义一个东西,然后选择而不是东西本身,而是它的父对象。 This isn't possible, and there is an interesting discussion on why it's not allowed here . 这是不可能的,并且对为什么它不允许一个有趣的讨论在这里 Basically, that type of thing would introduce huge performance problems in a browser. 基本上,这种类型的事情会在浏览器中引入巨大的性能问题。

However, you already must be using some Javascript to apply the active class to each <li> in .add-nav when it's clicked on. 但是,您必须已经使用一些Javascript将active类应用于单击时的.add-nav每个<li> It would only be a couple more lines of code to check which link is clicked and show/hide your .category-nav based on that. 只需几行代码即可检查单击了哪个链接,并根据该链接显示/隐藏您的.category-nav This is the best way to do it in my opinion. 我认为这是最好的方法。

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

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