css - 选择特定元素
问题描述
伙计们,如果我有很多段落,我不知道如何选择特定的类/id 选择器,而我只想选择一个特定的选择器。例如,如果我想选择第二个段落,我应该怎么做?<p>World</p>
我可以以某种方式选择它还是我需要添加另一个类。
例子:
HTML:
<div class="class">
<p>Hello</p>
<p>World</p>
</div>
如何选择第二段<p>World</p>
解决方案
看看伪类选择器:nth-of-type()
在这种情况下,你只需要做.class > div:nth-of-type(2)
.class > p:nth-of-type(2) {
background-color: red
}
<div class="class">
<p>Hello</p>
<p>World</p>
</div>
推荐阅读
- java - ProGuard 混淆后无法从 Library.jar 文件中导入类
- r - 相对频率条形图不加 1 -- geom_bar
- python - Pandas to_numpy() 产生一个列表数组。如何从中获取 2D numpy 数组?
- c# - 令牌标头中没有“iat”的 AspNetCore JWT 身份验证
- three.js - threejs - 如何将对象相对于鼠标旋转到从当前中心点设置的度数
- azure-devops-server-2019 - Azure Devops Release Server 2019 中的 YAML 模板使用
- arrays - 返回的数组在 Powershell 中有意外的元素
- android-studio - 字符串文字中的拼写错误在 Android Studio 中不起作用
- r - 在R中按某个值获取行
- mysql - 使用游标mysql返回一个值