CSS3 结合选择器与 OR 而不是 AND

标签 css css-selectors logical-operators

给定这个选择器:

body[class*="page-node-add-"][class~="page-node-edit"] {background:red;}

它将匹配一个主体,该主体具有一个包含 page-node-add- 的子字符串的类和一个恰好是 page-node-edit 的类/p>

我想说匹配第一个或第二个(但不是两者)。可能吗?

使用逗号的问题:

如果我有一个长选择器,例如:

body[class*="page-node-add-"] form.node-form > .field-type-field-collection > table > thead tr th,
body[class~="page-node-edit"] form.node-form > .field-type-field-collection > table > thead tr th
{...}

这是一个痛苦,我本以为 CSS3 可以解决这个问题,我想象的是这样的:

body([class*="page-node-add-"]):or([class~="page-node-edit"]) {background:red;}

谢谢

最佳答案

您需要使用逗号将它们分开:

body[class*="page-node-add-"], body[class~="page-node-edit"] {background:red;}

使用逗号的问题:

... 是你不能用逗号以外的任何其他方式来做到这一点。也许它可以用 Selectors 3 来补救,但不幸的是规范另有说明。这只能通过 Selectors 4 来解决。 ,要么是因为它直到最近才被提议,要么是它被提议但没有进入第 3 级。

在选择器的第 4 级中,您将能够执行如下操作:

body:matches([class*="page-node-add-"], [class~="page-node-edit"]) form.node-form > .field-type-field-collection > table > thead tr th
{...}

目前,这是在其最初提议的名称 :any() 下实现的,前缀为 :-moz-any(): -webkit-any()。但是在面向公众的 CSS 中使用 :any() 是毫无意义的,因为

  1. 只有 Gecko 和 WebKit 支持;和

  2. you have to duplicate your rulesets由于前缀选择器的处理方式,这不仅违背了 :matches() 选择器的预期目的,而且使事情变得更糟:

    body:-moz-any([class*="page-node-add-"], [class~="page-node-edit"]) form.node-form > .field-type-field-collection > table > thead tr th
    {...}
    body:-webkit-any([class*="page-node-add-"], [class~="page-node-edit"]) form.node-form > .field-type-field-collection > table > thead tr th
    {...}
    

换句话说,在实现将自身更新为标准化的 :matches() 之前,没有其他可行的解决方案(除了使用预处理器为您生成重复的选择器之外)。

关于CSS3 结合选择器与 OR 而不是 AND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10753174/

相关文章:

html - flex 盒: flex 基础:100%;不换行

javascript - 如何选择原型(prototype)中图像的所有链接

css - 表格中选定区域的边缘样式

c - C 中的逻辑运算符

jquery - CSS - 内联两个 div,第一个 Div 应该在左边,第二个 div 应该在中间位置

jquery - Bootstrap 时间选择器 - 如何在我们点击选择器时获取自定义时间?

c# - 如何使用带有 C# 的 Selenium 选择 img 标签标题或带有 td 的 alt

python - 如何使用 Selenium 和 Python 从 html 选择下拉列表中选择没有任何 ID 属性的选项

C 指针和 ||运营商

c# - C# 中的 OR 运算符