CSS不是选择器不工作

gre*_*ton 10 html css css-selectors

我的HTML是由wordpress生成的.

<div class="header-main">
            <h1 class="site-title"><a href="http://wp-themes.com/" rel="home">Theme Preview</a></h1>

            <div class="search-toggle active">
                <a href="#search-container" class="screen-reader-text">Search</a>
            </div>

            <nav id="primary-navigation" class="site-navigation primary-navigation" role="navigation">
                <h1 class="menu-toggle">Primary Menu</h1>
                <a class="screen-reader-text skip-link" href="#content">Skip to content</a>
                <div class="nav-menu"><ul><li class="page_item page-item-2"><a href="http://wp-themes.com/?page_id=2">About</a></li><li class="page_item page-item-46 page_item_has_children"><a href="http://wp-themes.com/?page_id=46">Parent Page</a><ul class="children"><li class="page_item page-item-49"><a href="http://wp-themes.com/?page_id=49">Sub-page</a></li></ul></li></ul></div>
            </nav>
        </div>
Run Code Online (Sandbox Code Playgroud)

我想要隐藏所有元素,但.page-item-2我使用的是:

.header-main .nav-menu li:not(.page-item-2) {
    display:none;
}
Run Code Online (Sandbox Code Playgroud)

这有效,但我也想.page-item-46从选择器中排除:

.header-main .nav-menu li:not(.page-item-2) :not(.page-item-46) {
    display:none;
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

Jos*_*ier 17

该元素.page-item-46不是后代,因此您将删除:not伪类之间的空格:

.header-main .nav-menu li:not(.page-item-2) :not(.page-item-46) {
                            /* remove this ^  */
    display:none;
}
Run Code Online (Sandbox Code Playgroud)

这里的例子


有关更基本的示例,请考虑以下事项:

<ul>
    <li class="one">one..</li>
    <li class="two">two</li>
    <li class="three">three</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

使用以下内容将排除.one/ .two选择:( 示例)

ul li:not(.one):not(.two) {
    color:red;
}
Run Code Online (Sandbox Code Playgroud)

以下不是:( 示例)

ul li:not(.one) :not(.two) {
    color:red;
}
Run Code Online (Sandbox Code Playgroud)

这也不是:( 例子)

ul li:not(.one,.two) {
    color:red;
}
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为它基本上选择了所有元素,因为两个选择器不是互斥的.(例)

ul li:not(.one),
ul li:not(.two) {
    color:red;
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用jQuery并且想知道为什么`:not(.one,.two)`在这里不起作用,请参阅http://stackoverflow.com/questions/10711730/whats-the-difference-in-the-not-选择器之间-jquery的和 - CSS (2认同)