css - 如何找到指定顺序的某类名元素
问题描述
<p class='item2'> <p class='tcc'><p class='icon'></p><p>2017.07.07</p> </p> <p class='link'> </p> <p class='tcc'><p class='icon'></p><p>12个月</p> </p> <p class='link'> </p> <p class='tcc'><p class='icon'></p><p>2017.07.08</p> </p></p>
我想通过
item2 .tcc:nth-of-type(2) .icon{}
给第二个tcc类名的元素添加样式,为啥会无效的。我后来想了一下,nth-of-type这类伪类选择器前都是要元素标签的吗?网上的教程好像都是,不能是类名吗?
问题解答
回答1:因为 <p class='link'></p> 也是 p 所以 第二个 tcc 其实是 nth-of-type(3)
回答2:且不说 nth-of-type 需不需要显式指定标签,:nth-of-type(n) 是指选择父元素中具有指定类型的第 n 个子元素,而你的第二个 .tcc 是 .item2 的第 3 个子元素,而不是第 2 个,或许你应该写成 .item2 .tcc:nth-of-type(3) .icon {}
回答3:item2 .tcc:nth-of-type(2) .icon{}
替换为
item2 .tcc:nth-child(3) .icon{}
相关文章:
1. HTML5禁止img预览该怎么解决?2. android - 优酷的安卓及苹果app还在使用flash技术吗?3. python - celery 如何解决worker和client代码同步问题4. mysqld无法关闭5. docker 17.03 怎么配置 registry mirror ?6. javascript - mongodb怎么设置用户的账户不重复?7. javascript - onclick事件点击不起作用8. 请问各位java自带类的一些问题9. javascript - "CommonsChunkPlugin now only takes a single argument.。。。。10. node.js - 微信的自动回复问题

网公网安备