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