在 CSS 中递归选择所有子元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4910077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:39:23  来源:igfitidea点击:

Select all child elements recursively in CSS

csscss-selectors

提问by clarkk

How can you select all child elements recursively?

如何递归地选择所有子元素?

div.dropdown, div.dropdown > * {
    color: red;
}

This class only throws a class on the defined className and all immediate children. How can you, in a simple way, pick all childNodes like this:

这个类只在定义的 className 和所有直接子级上抛出一个类。您如何以一种简单的方式选择所有 childNode,如下所示:

div.dropdown, 
div.dropdown > *, 
div.dropdown > * > *, 
div.dropdown > * > * > *, 
div.dropdown > * > * > * > * {
    color: red;
}

回答by anroesti

Use a white spaceto match all descendants of an element:

使用空格来匹配元素的所有后代:

div.dropdown * {
    color: red;
}

x ymatches every element ythat is inside x, however deeply nested it may be - children, grandchildren and so on.

x y匹配x内部的每个元素y,无论它嵌套的深度如何 - 子元素、孙子元素等等。

The asterisk *matches any element.

星号*匹配任何元素。

Official Specification: CSS 2.1: Chapter 5.5: Descendant Selectors

官方规范:CSS 2.1:第 5.5 章:后代选择器

回答by Abdennour TOUMI

The rule is as following :

规则如下:

A B 

B as a descendant of A

B 作为 A 的后代

A > B 

B as a child of A

B 作为 A 的孩子

So

所以

div.dropdown *

and not

并不是

div.dropdown > *