简体   繁体   English

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

[英]CSS selectors for excluding by attribute presence

The document I am parsing has the following two styles of td tags (among other instances of td tags): 我解析该文件有以下两种款式的td标签(的其他实例中td标签):

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

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

How do I write a selector which only selects the second type, and exclude all the other td tags? 如何编写仅选择第二种类型的选择器,并排除所有其他td标签?

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

excludes the first type, includes the second type plus all other td tags as well. 排除第一种类型,包括第二种类型以及所有其他td标签。

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

excludes all other td tags, but includes both types above. 排除所有其他td标签,但包括上述两种类型。

You can simply combine the :not selector with the other attribute selectors: 您可以简单地将:not selector与其他属性选择器组合:

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

You could even put the :not after the others (it doesn't need to be right next to the element name): 你甚至可以把:not其他的(它不需要在元素名称旁边):

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

These will both select all td elements that have bgcolor="#A1C87A" and nowrap but don't have align="right" , which is what you're after. 这些都将选择所有具有bgcolor="#A1C87A"nowrap但没有align="right" td元素,这就是你所追求的。

td:not([align="right"][nowrap][bgcolor])应该足以满足您的需求

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM