html - 选择高级选择器的第 n 个 child

标签 html css

如何使用不是所述子项的直接父项的选择器的伪类 nth-child

HTML

<div id="main">
</div>

CSS

.test {
  width: 100px;
  height: 100px;
  background: red;
}

#main .test:nth-child(even) {
  background: green;
}

JS

// doesnt work
for (var i = 0; i < 3; i++) {
  $('#main').append('<div><div class="test"></div></div>')
}
// works
for (var i = 0; i < 3; i++) {
  $('#main').append('<div class="test"></div>')
}

JSFiddle

最佳答案

如果要选择.test div,需要选择#main的每个偶数子元素,然后选择.test内。

CSS

#main div:nth-child(even) .test {
  background: green;
}

JSFiddle

关于html - 选择高级选择器的第 n 个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38925917/

相关文章:

html - CSS 媒体查询不适用于宽度为 1000 像素及以上的设备

html - 服务器呈现与本地主机不同的样式

css - 切换到设备 View 时,Chrome 开发人员工具不加载 @media 查询

css - 通过 cognitoConfig.yaml 文件自定义 AWS cognito 登录页面 CSS

javascript - 将一个高度加到另一个高度上

javascript - d3 中的响应轴值

html - 使用媒体查询和断点操作网格列布局

html - margin 不能正常工作?填充做 margin 的工作?

html - 固定位置的灵活div

CSS Shape 或 SVG mask,哪个是响应式设计的最佳方法?