CSS选择器仅适用于两个类,没有其他选择器
问题说明
我正在寻找某种CSS选择器,该选择器仅从仅具有这两个特定类的表中选择某些单元格,而没有其他选择.这是一个更加清晰的示例.
I am looking for some kind of CSS selector that will only select certain cells from a table that only have these two specific classes and nothing else. Here is an example for a bit more clarity.
因此在此表中,我想从行中选择单元格A和D进行样式设置,但是选择它们的唯一可区分方法是它们除了.abc和.xyz之外没有其他任何类.所以从本质上讲,我需要筛选出具有其他任何类(即.def,.ghi)的单元格.
So in this table i want to select cells A and D for styling from the row but the only distinguishable way to select them is that they don't have any classes other than .abc and .xyz. So essentially i need to fitler out any cells that have any other classes i.e. .def, .ghi.
<tr>
<td class="abc xyz">A</td>
<td class="abc xyz def">B</td>
<td class="abc xyz ghi">C</td>
<td class="abc xyz">D</td>
</tr>
现在我知道我可以使用not选择器来解决这个问题,但是这些类是从外部源注入到表中的,可以添加新的类,而我真的不想一直添加到不断增长的类列表中忽略.
Now i know i could go about it using the not selector but these classes are injected into table from an external source and new classes could be added and i don't really want to keep adding to an ever growing list of classes to ignore.
td.abc.xyz:not(.def),
td.abc.xyz:not(.ghi) {
/* Style to apply */
}
因此,如果有人可以将我的正确方向指向任何有帮助的地方,将不胜感激.
So if anyone could point me in the right direction to anything that could help, it would be greatly appreciated.
正确答案
您可以考虑属性选择器,但要注意空格和顺序:
You can consider attribute selector but pay attention to whitespaces and order:
[class="abc xyz"],
[class="xyz abc"] {
color:red;
}
<table>
<tr>
<td class="abc xyz">A</td>
<td class="abc xyz def">B</td>
<td class="abc xyz ghi">C</td>
<td class="abc xyz">D</td>
<td class="xyz abc">D</td>
<td class="abc xyz ">this will fail</td>
</tr>
</table>
如果可以添加一些JS,则可以使用 trim
If you can add some JS you can avoid the whitespace issue using trim
var td=document.querySelectorAll('td');
for(var i=0;i<td.length;i )
td[i].className=td[i].className.trim();
[class="abc xyz"],
[class="xyz abc"] {
color:red;
}
<table>
<tr>
<td class="abc xyz">A</td>
<td class="abc xyz def">B</td>
<td class="abc xyz ghi">C</td>
<td class="abc xyz">D</td>
<td class="xyz abc">D</td>
<td class="abc xyz ">this will be good</td>
</tr>
</table>
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhchabab
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20