html - css 选择器,带有子元素(及其子元素)的父元素

标签 html css css-selectors

谁能解释为什么这不起作用?在 div>ol:first-child 上?

div>ul:first-child
{
    background:yellow;
}

顶部的样式适用于 ul。高亮它。如果我将 >ul 更改为 >ol.. 什么也不会发生

<div>
<ul>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ul>
<ol>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Coca Cola</li>
</ol>
</div>

http://jsfiddle.net/mFMuq/

最佳答案

来自 w3schools.com first-child selector :

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

ol 不是其父项的第一个子项,它是其父项的第一个 ol 实际上并不重要。如果您交换 ol 和 ul,则 ol 有效,但 ul 无效。

如果你想定位第一个 ol 然后使用 :first-of-type 选择器。 JSFiddle

关于html - css 选择器,带有子元素(及其子元素)的父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13413451/

相关文章:

html - 输入类型 = 日期的 W3C 验证错误

javascript - 为什么 IE 和 FF 不会为输入类型日期返回正确的属性或类型

javascript - 如何让 clickcount 函数从 0 以外的数字开始

html - CSS 导入字体

css - 不要为有序或无序列表显示数字/元素符号

html css 下拉菜单不起作用

python - CSS Selector 获取元素属性值

html - 什么时候应用这些 CSS 属性 : ul#nav li. current a, ul#nav li :hover a?

html - 仅使用CSS即可实现点击事件

html - 当屏幕尺寸较小时,第 n 个 child 不归因