V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Can I use?
http://caniuse.com/
Less
http://lesscss.org
Cool Libraries
Bootstrap from Twitter
Jetstrap
deeporist
V2EX  ›  CSS

CSS 选择器写法请教(反正我是万策尽矣了)

  •  
  •   deeporist · 2017-10-18 12:20:46 +08:00 · 2936 次点击
    这是一个创建于 2375 天前的主题,其中的信息可能已经有所发展或是发生改变。

    A: div class="R clearfix " data-field="xxxx"

    B: div class="R clearfix v9d1365e44 "

    C: div class="R clearfix " data-field="xxxx"

    提取 B, "v9d1365e44"是一串随机字符, 由字母和数字构成.

    首先 B 出现在随机位置没办法用第几子元素定位, 以整个 class 字串来定位的话只要随机字串一变就失效.

    用:not[class$="clearfix"]也不行, 因为在有的页面 A 和 C 也不是以 clearfix 结尾的(cbright 还是啥的).

    我还能想到的就是 regex 去匹配那串随机字符, 但是怎么写进 css 选择器里我就不知道了, 怎么查都没查到, 更不用说 regex 我都是睡一觉就全忘得一干二净......

    6 条回复    2017-10-18 13:24:20 +08:00
    Carseason
        1
    Carseason  
       2017-10-18 13:02:43 +08:00 via iPhone
    用^匹配开始,或者正则
    Troevil
        2
    Troevil  
       2017-10-18 13:09:04 +08:00
    难道不是 :not([data-field])
    cowpea
        3
    cowpea  
       2017-10-18 13:11:46 +08:00
    是你自己页面的话直接给 B 加上别的 css 或者 id 不就可以了。
    你控制不了的页面可以先获取到所有 css 再去判断长度
    https://gist.github.com/onionc/2990e5cb3dd874037199d7b982c19d45
    lianyue
        4
    lianyue  
       2017-10-18 13:13:43 +08:00
    [class^="R clearfix "]

    这样
    cowpea
        5
    cowpea  
       2017-10-18 13:14:47 +08:00
    上面打错,加上 class 不是 css
    Chingim
        6
    Chingim  
       2017-10-18 13:24:20 +08:00 via Android
    .R.clearfix:not([data-field]) {
    color:red;
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5633 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 06:23 · PVG 14:23 · LAX 23:23 · JFK 02:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.