吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1469|回复: 32
收起左侧

[求助] CSS选择器的问题

  [复制链接]
cqwcns 发表于 2021-10-18 21:40
CSS如何可以按类名,选中非第一个元素。


以以下Demo为例,我希望选择“第2个item”、“第3个item”、“第4个item”、“第5个item”。
不知道要怎么选,谢谢指教。

[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .content .item:not(first-child) {
            background-color: yellow;
        }
    </style>

</head>

<body>

    <div class="content">
        <p>第1个元素</p>
        <p>第2个元素</p>
        <p class="item">第3个元素,而且是第1个item</p>
        <p>第4个元素</p>
        <p class="item">第5个元素,而且是第2个item</p>
        <p class="item">第6个元素,而且是第3个item</p>
        <p class="item">第7个元素,而且是第4个item</p>
        <p>第8个元素</p>
        <p class="item">第9个元素,而且是第5个item</p>
        <p>第10个元素</p>


    </div>



</body>

</html>

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
yan182 + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

13599383608 发表于 2021-10-18 22:00
  .item:nth-child(2)
  .item:nth-child(3)
  .item:nth-child(4)
  .item:nth-child(5)
试试看
 楼主| cqwcns 发表于 2021-10-18 22:07
13599383608 发表于 2021-10-18 22:00
.item:nth-child(2)
  .item:nth-child(3)
  .item:nth-child(4)

谢谢回复。

但这个不行,等于把代码写死了。

我以上只是一个例子,实际情况有各种各样的,item数量也不定。
头像被屏蔽
52pengcheng 发表于 2021-10-18 22:19
 楼主| cqwcns 发表于 2021-10-18 22:26

谢谢回复。

当然不是,关键是item会随机夹杂着其他元素,难点就在这里。

就是用+号兄弟选择器的实现不了。
ljl9090 发表于 2021-10-18 22:38
[HTML] 纯文本查看 复制代码
<style type="text/css">
    .content>p.item+p+p.item {
        background-color: yellow;
    }
    .content>p.item+p.item {
        background-color: yellow;
    }
</style>
墨墨殿下 发表于 2021-10-18 22:42
那你用jq得了
SuiY 发表于 2021-10-18 23:13
使用id选择器
徒想er 发表于 2021-10-18 23:33
你的需求都没说清楚。。
Lohraki 发表于 2021-10-19 00:00
可以尝试jQuery
$('.content .item').each(function () {
        console.log(($(this).val()))
})
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 00:43

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表