Html CSS 子选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7615866/
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 10:52:31  来源:igfitidea点击:

CSS children Selector

htmlcsscss-selectors

提问by jdross

Lets say I have the following HTML

假设我有以下 HTML

<div id="div1">
....
<span class="innercontents">...</span>
....
</div>

Can I select just the child of the parent ID?

我可以只选择父 ID 的孩子吗?

Could I do something like

我可以做类似的事情吗

#div1 span
{
...
}

Thanks for any help.

谢谢你的帮助。

Sorry for any confusion. I should have been more clear. In the above example I would like to just select the tags that fall under that specific

很抱歉有任何混淆。我应该更清楚。在上面的例子中,我只想选择属于该特定的标签

回答by John Hartsock

#div1 > .innercontents /* child selector */

The above will select these ids from the following HTML: c and d

以上将从以下 HTML 中选择这些 id: c and d

<div id="div1">
   <div id="a">
     <span id="b" class="innercontents"></span>
   </div>
   <span id="c" class="innercontents"></span>
   <span id="d" class="innercontents"></span>
</div>

if you want all descendents selected such as b, c, and dfrom the above HTML then use

如果您希望b, c, and d从上面的 HTML 中选择所有后代,然后使用

#div1 .innercontents 

回答by CamelCamelCamel

Yes. #div1 > .innercontents. This is the immediate descendent selector, or child selector.

是的。#div1 > .innercontents. 这是直接后代选择器或子选择器。

This is the best reference for CSS selectors: http://www.w3.org/TR/css3-selectors/#selectors

这是 CSS 选择器的最佳参考:http: //www.w3.org/TR/css3-selectors/#selectors