CSS选择类的多个后代 [英] CSS select multiple descendants of class

查看:282
本文介绍了CSS选择类的多个后代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在CSS中选择多个具有某个类的祖先的标签吗?例如:

  table.exams caption,tbody,tfoot,thead,tr,th,td 



如果没有,是否有办法选择该类的所有后代?

解决方案


可以在CSS中选择多个具有某个类的祖先的标签吗?


目前,不是没有复制选择器和指定所有的,不幸的是:

 表。考试题目,
table.exams tbody,
table.exams tfoot,
table.exams thead,
table.exams tr,
table.exams th,
table.exams td

直到Selectors 3最终确定后,伪类符号来做到这一点,并且最近才开始出现基本的实现。请参阅此答案



在不久的将来,如果:matches()让Selectors 4和浏览器开始实现你可以这样做:

  table.exams:matches(caption,tbody,tfoot,thead, tr,th,td)




如果没有,选择该类的所有后代?


你可以简单地使用星号 * ,表示任何元素。考虑到你的选择器中有 th td ,你可能意味着所有后代而不是<$ c的所有子$ c> table.exams ,因此不要使用> ,而是使用空格:

  table.exams * 

避免这样做。如果可以,请尽量指定您尝试选择哪种后代。


Is it possible to select multiple tags that have an ancestor of a certain class in CSS? e.g:

table.exams caption, tbody, tfoot, thead, tr, th, td

If not, is there a way to select all descendants of that class?

解决方案

Is it possible to select multiple tags that have an ancestor of a certain class in CSS?

Currently, not without duplicating the selector and specifying all of them, unfortunately:

table.exams caption, 
table.exams tbody, 
table.exams tfoot, 
table.exams thead, 
table.exams tr, 
table.exams th, 
table.exams td

It was only until late after Selectors 3 was being finalized that they proposed a pseudo-class notation to do this, and it was only recently that basic implementations have started showing up. See this answer for a little history lesson.

In the distant future, if :matches() makes it to Selectors 4 and browsers start implementing it, you may be able to do something like this:

table.exams :matches(caption, tbody, tfoot, thead, tr, th, td)

If not, is there a way to select all descendants of that class?

Well, you can simply use an asterisk *, which represents any element. Given that you have th and td in your selector, you probably mean all descendants rather than all children of table.exams, so don't use >, use a space instead:

table.exams *

But really, avoid doing this. If you can, do your utmost to specify what kind of descendants you're trying to select.

这篇关于CSS选择类的多个后代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆