- /* 第一个 li 元素 */
- li:first-child{
- color: red;
- }
- /* 最后一个元素 */
- li:last-child{
- color: green;
- }
- /* 获取第 10 个元素 */
- li:nth-child(10){
- color: orange;
- }
- /* 获取倒数第 3 个 li 元素 */
- li:nth-last-child(3){
- color: purple;
- }
- /* 获取索引顺序为 6 的倍数的 li 元素 */
- li:nth-child(6n){
- text-decoration: underline;
- border: 1px solid red;
- }
- /* 获取所有索引为偶数的 li 元素 */
- li:nth-child(even){
- border: 1px solid black;
- }
- /* 获取前 5 个 li 元素 */
- li:nth-child(-n+5){
- background-color: #ddd;
- }
来源: http://www.bubuko.com/infodetail-3098019.html