CSS 个用于按属性存在排除的选择器

CSS selectors for excluding by attribute presence

我正在解析的文档具有以下两种样式的 td 标签(以及 td 标签的其他实例):

<td align="right" nowrap bgcolor="#A1C87A">...</td>

<td align="right" nowrap>...</td>

如何编写一个只选择第二种类型并排除所有其他 td 标签的选择器?

document.css('td:not([bgcolor="#A1C87A"])')

不包括第一种类型,包括第二种类型以及所有其他 td 标签。

document.css('td[align="right"][nowrap]')

排除所有其他 td 标签,但包括上述两种类型。

td:not([align="right"][nowrap][bgcolor])应该够你想要的了

td[nowrap][bgcolor] ~ td[nowrap] { code };

您可以简单地将 :not selector 与其他属性 selector:

组合
document.css('td:not([bgcolor="#A1C87A"])[align="right"][nowrap]')

您甚至可以将 :not 放在其他元素之后(它不需要紧挨着元素名称):

document.css('td[align="right"][nowrap]:not([bgcolor="#A1C87A"])')

这些都将 select 具有 bgcolor="#A1C87A"nowrap 但没有 align="right" 的所有 td 元素之后。