css - 我怎样才能将CSS选择器组合在一起

标签 css css-selectors

是否可以将 :not() 选择器与 :nth-of-type(1) 选择器一起使用?

即 我想选择第一个没有标题“something”的

<html>
    <head>
        <style type="text/css">
            p
            {
                color:#000000;
            }
            p:not([title=something]):nth-of-type(1)
            {
                color:#ff0000;
            }
        </style>
    </head>
    <body>

        <h1>This is a heading</h1>

        <p title="something">This is a paragraph.</p>
        <p>This is another paragraph.</p>
        <p>This is another paragraph.</p>

        <div>This is some text in a div element.</div>


    </body>
</html>

最佳答案

nth-of-type作用于原始选择器( p ),它不作用于 p:not([title=something]) 的结果.

p:not([title=something]):nth-of-type(1)

这就是说,找到<p>没有“someting”的标题也是第一个<p>在页面上。这没有找到任何元素作为第一个 <p>标题为“某物”。

你要的是第一个<p>不包含标题“某物”。我不知道 CSS 是否有这样做的好方法。

如果你愿意使用 jQuery,你可以这样做:

$('p:not([title="something"]):eq(0)')

或:

$('p').not('[title="something"]').eq(0)

关于css - 我怎样才能将CSS选择器组合在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9523760/

相关文章:

css - 选择基于 "class"声明的存在

css - 伪元素之前的动态样式 - Angular 5

javascript - 在顶部检测滚动事件(移动)

javascript - 如何在标记之间获取代码

css - 对多个 HTML 标签使用相同的 ID?

html - 如何删除第三方选择器?

html - 如何在父 div 中对齐 3 个子 div

internet-explorer-8 - CSS3 饼图 : divs with border-radius flickering on mouseover

css - 您如何对相似的第 n 个子选择器进行分组?

javascript - 在元素列表中,将编号索引放在 id 中还是使用索引选择器?