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

是否可以将 :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)

https://stackoverflow.com/questions/9523760/

相关文章:

optimization - 为什么在没有 mem2reg 的情况下优化过程不起作用?

html - onclick按钮边框颜色改变

ruby-on-rails - 创建 Rails 音乐播放器应用程序(类似于 Rdio)

django - 如何在 Django 开发中提供静态文件

python - 如何设置浏览器为 webbrowser 打开 localhost?

matlab - 不同长度的向量之间的相关性

oracle - 如何以编程方式在 PL/SQL 中设置表名?

cuda - nVIDIA CUDA 驱动程序究竟做了什么?

ajax - JSF f :ajax listener not called

asp.net - 如何检查 Amazon S3 存储桶中文件夹的子文件夹大小